garbage.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """Garbage collection thread for representing zmq refcount of Python objects
  2. used in zero-copy sends.
  3. """
  4. # Copyright (C) PyZMQ Developers
  5. # Distributed under the terms of the Modified BSD License.
  6. import atexit
  7. import struct
  8. from os import getpid
  9. from collections import namedtuple
  10. from threading import Thread, Event, Lock
  11. import warnings
  12. import zmq
  13. gcref = namedtuple('gcref', ['obj', 'event'])
  14. class GarbageCollectorThread(Thread):
  15. """Thread in which garbage collection actually happens."""
  16. def __init__(self, gc):
  17. super(GarbageCollectorThread, self).__init__()
  18. self.gc = gc
  19. self.daemon = True
  20. self.pid = getpid()
  21. self.ready = Event()
  22. def run(self):
  23. # detect fork at beginning of the thread
  24. if getpid is None or getpid() != self.pid:
  25. self.ready.set()
  26. return
  27. try:
  28. s = self.gc.context.socket(zmq.PULL)
  29. s.linger = 0
  30. s.bind(self.gc.url)
  31. finally:
  32. self.ready.set()
  33. while True:
  34. # detect fork
  35. if getpid is None or getpid() != self.pid:
  36. return
  37. msg = s.recv()
  38. if msg == b'DIE':
  39. break
  40. fmt = 'L' if len(msg) == 4 else 'Q'
  41. key = struct.unpack(fmt, msg)[0]
  42. tup = self.gc.refs.pop(key, None)
  43. if tup and tup.event:
  44. tup.event.set()
  45. del tup
  46. s.close()
  47. class GarbageCollector(object):
  48. """PyZMQ Garbage Collector
  49. Used for representing the reference held by libzmq during zero-copy sends.
  50. This object holds a dictionary, keyed by Python id,
  51. of the Python objects whose memory are currently in use by zeromq.
  52. When zeromq is done with the memory, it sends a message on an inproc PUSH socket
  53. containing the packed size_t (32 or 64-bit unsigned int),
  54. which is the key in the dict.
  55. When the PULL socket in the gc thread receives that message,
  56. the reference is popped from the dict,
  57. and any tracker events that should be signaled fire.
  58. """
  59. refs = None
  60. _context = None
  61. _lock = None
  62. url = "inproc://pyzmq.gc.01"
  63. def __init__(self, context=None):
  64. super(GarbageCollector, self).__init__()
  65. self.refs = {}
  66. self.pid = None
  67. self.thread = None
  68. self._context = context
  69. self._lock = Lock()
  70. self._stay_down = False
  71. self._push = None
  72. self._push_mutex = None
  73. atexit.register(self._atexit)
  74. @property
  75. def context(self):
  76. if self._context is None:
  77. if Thread.__module__.startswith('gevent'):
  78. # gevent has monkey-patched Thread, use green Context
  79. from zmq import green
  80. self._context = green.Context()
  81. else:
  82. self._context = zmq.Context()
  83. return self._context
  84. @context.setter
  85. def context(self, ctx):
  86. if self.is_alive():
  87. if self.refs:
  88. warnings.warn("Replacing gc context while gc is running", RuntimeWarning)
  89. self.stop()
  90. self._context = ctx
  91. def _atexit(self):
  92. """atexit callback
  93. sets _stay_down flag so that gc doesn't try to start up again in other atexit handlers
  94. """
  95. self._stay_down = True
  96. self.stop()
  97. def stop(self):
  98. """stop the garbage-collection thread"""
  99. if not self.is_alive():
  100. return
  101. self._stop()
  102. def _stop(self):
  103. push = self.context.socket(zmq.PUSH)
  104. push.connect(self.url)
  105. push.send(b'DIE')
  106. push.close()
  107. if self._push:
  108. self._push.close()
  109. self._push = None
  110. self._push_mutex = None
  111. self.thread.join()
  112. self.context.term()
  113. self.refs.clear()
  114. self.context = None
  115. @property
  116. def _push_socket(self):
  117. """The PUSH socket for use in the zmq message destructor callback.
  118. """
  119. if not self.is_alive() or self._push is None:
  120. self._push = self.context.socket(zmq.PUSH)
  121. self._push.connect(self.url)
  122. return self._push
  123. def start(self):
  124. """Start a new garbage collection thread.
  125. Creates a new zmq Context used for garbage collection.
  126. Under most circumstances, this will only be called once per process.
  127. """
  128. if self.thread is not None and self.pid != getpid():
  129. # It's re-starting, must free earlier thread's context
  130. # since a fork probably broke it
  131. self._stop()
  132. self.pid = getpid()
  133. self.refs = {}
  134. self.thread = GarbageCollectorThread(self)
  135. self.thread.start()
  136. self.thread.ready.wait()
  137. def is_alive(self):
  138. """Is the garbage collection thread currently running?
  139. Includes checks for process shutdown or fork.
  140. """
  141. if (getpid is None or
  142. getpid() != self.pid or
  143. self.thread is None or
  144. not self.thread.is_alive()
  145. ):
  146. return False
  147. return True
  148. def store(self, obj, event=None):
  149. """store an object and (optionally) event for zero-copy"""
  150. if not self.is_alive():
  151. if self._stay_down:
  152. return 0
  153. # safely start the gc thread
  154. # use lock and double check,
  155. # so we don't start multiple threads
  156. with self._lock:
  157. if not self.is_alive():
  158. self.start()
  159. tup = gcref(obj, event)
  160. theid = id(tup)
  161. self.refs[theid] = tup
  162. return theid
  163. def __del__(self):
  164. if not self.is_alive():
  165. return
  166. try:
  167. self.stop()
  168. except Exception as e:
  169. raise (e)
  170. gc = GarbageCollector()