queues.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ###############################################################################
  2. # Queue and SimpleQueue implementation for loky
  3. #
  4. # authors: Thomas Moreau, Olivier Grisel
  5. #
  6. # based on multiprocessing/queues.py (16/02/2017)
  7. # * Add some compatibility function for python2.7 and 3.3 and makes sure
  8. # it uses the right synchronization primitive.
  9. # * Add some custom reducers for the Queues/SimpleQueue to tweak the
  10. # pickling process. (overload Queue._feed/SimpleQueue.put)
  11. #
  12. import os
  13. import sys
  14. import errno
  15. import weakref
  16. import threading
  17. from multiprocessing import util
  18. from multiprocessing import connection
  19. from multiprocessing.synchronize import SEM_VALUE_MAX
  20. from multiprocessing.queues import Full
  21. from multiprocessing.queues import _sentinel, Queue as mp_Queue
  22. from multiprocessing.queues import SimpleQueue as mp_SimpleQueue
  23. from .reduction import loads, dumps
  24. from .context import assert_spawning, get_context
  25. __all__ = ['Queue', 'SimpleQueue', 'Full']
  26. class Queue(mp_Queue):
  27. def __init__(self, maxsize=0, reducers=None, ctx=None):
  28. if sys.version_info[:2] >= (3, 4):
  29. super().__init__(maxsize=maxsize, ctx=ctx)
  30. else:
  31. if maxsize <= 0:
  32. # Can raise ImportError (see issues #3770 and #23400)
  33. maxsize = SEM_VALUE_MAX
  34. if ctx is None:
  35. ctx = get_context()
  36. self._maxsize = maxsize
  37. self._reader, self._writer = connection.Pipe(duplex=False)
  38. self._rlock = ctx.Lock()
  39. self._opid = os.getpid()
  40. if sys.platform == 'win32':
  41. self._wlock = None
  42. else:
  43. self._wlock = ctx.Lock()
  44. self._sem = ctx.BoundedSemaphore(maxsize)
  45. # For use by concurrent.futures
  46. self._ignore_epipe = False
  47. self._after_fork()
  48. if sys.platform != 'win32':
  49. util.register_after_fork(self, Queue._after_fork)
  50. self._reducers = reducers
  51. # Use custom queue set/get state to be able to reduce the custom reducers
  52. def __getstate__(self):
  53. assert_spawning(self)
  54. return (self._ignore_epipe, self._maxsize, self._reader, self._writer,
  55. self._reducers, self._rlock, self._wlock, self._sem,
  56. self._opid)
  57. def __setstate__(self, state):
  58. (self._ignore_epipe, self._maxsize, self._reader, self._writer,
  59. self._reducers, self._rlock, self._wlock, self._sem,
  60. self._opid) = state
  61. self._after_fork()
  62. # Overload _start_thread to correctly call our custom _feed
  63. def _start_thread(self):
  64. util.debug('Queue._start_thread()')
  65. # Start thread which transfers data from buffer to pipe
  66. self._buffer.clear()
  67. self._thread = threading.Thread(
  68. target=Queue._feed,
  69. args=(self._buffer, self._notempty, self._send_bytes,
  70. self._wlock, self._writer.close, self._reducers,
  71. self._ignore_epipe, self._on_queue_feeder_error, self._sem),
  72. name='QueueFeederThread'
  73. )
  74. self._thread.daemon = True
  75. util.debug('doing self._thread.start()')
  76. self._thread.start()
  77. util.debug('... done self._thread.start()')
  78. # On process exit we will wait for data to be flushed to pipe.
  79. #
  80. # However, if this process created the queue then all
  81. # processes which use the queue will be descendants of this
  82. # process. Therefore waiting for the queue to be flushed
  83. # is pointless once all the child processes have been joined.
  84. created_by_this_process = (self._opid == os.getpid())
  85. if not self._joincancelled and not created_by_this_process:
  86. self._jointhread = util.Finalize(
  87. self._thread, Queue._finalize_join,
  88. [weakref.ref(self._thread)],
  89. exitpriority=-5
  90. )
  91. # Send sentinel to the thread queue object when garbage collected
  92. self._close = util.Finalize(
  93. self, Queue._finalize_close,
  94. [self._buffer, self._notempty],
  95. exitpriority=10
  96. )
  97. # Overload the _feed methods to use our custom pickling strategy.
  98. @staticmethod
  99. def _feed(buffer, notempty, send_bytes, writelock, close, reducers,
  100. ignore_epipe, onerror, queue_sem):
  101. util.debug('starting thread to feed data to pipe')
  102. nacquire = notempty.acquire
  103. nrelease = notempty.release
  104. nwait = notempty.wait
  105. bpopleft = buffer.popleft
  106. sentinel = _sentinel
  107. if sys.platform != 'win32':
  108. wacquire = writelock.acquire
  109. wrelease = writelock.release
  110. else:
  111. wacquire = None
  112. while 1:
  113. try:
  114. nacquire()
  115. try:
  116. if not buffer:
  117. nwait()
  118. finally:
  119. nrelease()
  120. try:
  121. while 1:
  122. obj = bpopleft()
  123. if obj is sentinel:
  124. util.debug('feeder thread got sentinel -- exiting')
  125. close()
  126. return
  127. # serialize the data before acquiring the lock
  128. obj_ = dumps(obj, reducers=reducers)
  129. if wacquire is None:
  130. send_bytes(obj_)
  131. else:
  132. wacquire()
  133. try:
  134. send_bytes(obj_)
  135. finally:
  136. wrelease()
  137. # Remove references early to avoid leaking memory
  138. del obj, obj_
  139. except IndexError:
  140. pass
  141. except BaseException as e:
  142. if ignore_epipe and getattr(e, 'errno', 0) == errno.EPIPE:
  143. return
  144. # Since this runs in a daemon thread the resources it uses
  145. # may be become unusable while the process is cleaning up.
  146. # We ignore errors which happen after the process has
  147. # started to cleanup.
  148. if util.is_exiting():
  149. util.info('error in queue thread: %s', e)
  150. return
  151. else:
  152. queue_sem.release()
  153. onerror(e, obj)
  154. def _on_queue_feeder_error(self, e, obj):
  155. """
  156. Private API hook called when feeding data in the background thread
  157. raises an exception. For overriding by concurrent.futures.
  158. """
  159. import traceback
  160. traceback.print_exc()
  161. if sys.version_info[:2] < (3, 4):
  162. # Compat for python2.7/3.3 that use _send instead of _send_bytes
  163. def _after_fork(self):
  164. super(Queue, self)._after_fork()
  165. self._send_bytes = self._writer.send_bytes
  166. class SimpleQueue(mp_SimpleQueue):
  167. def __init__(self, reducers=None, ctx=None):
  168. if sys.version_info[:2] >= (3, 4):
  169. super().__init__(ctx=ctx)
  170. else:
  171. # Use the context to create the sync objects for python2.7/3.3
  172. if ctx is None:
  173. ctx = get_context()
  174. self._reader, self._writer = connection.Pipe(duplex=False)
  175. self._rlock = ctx.Lock()
  176. self._poll = self._reader.poll
  177. if sys.platform == 'win32':
  178. self._wlock = None
  179. else:
  180. self._wlock = ctx.Lock()
  181. # Add possiblity to use custom reducers
  182. self._reducers = reducers
  183. def close(self):
  184. self._reader.close()
  185. self._writer.close()
  186. # Use custom queue set/get state to be able to reduce the custom reducers
  187. def __getstate__(self):
  188. assert_spawning(self)
  189. return (self._reader, self._writer, self._reducers, self._rlock,
  190. self._wlock)
  191. def __setstate__(self, state):
  192. (self._reader, self._writer, self._reducers, self._rlock,
  193. self._wlock) = state
  194. if sys.version_info[:2] < (3, 4):
  195. # For python2.7/3.3, overload get to avoid creating deadlocks with
  196. # unpickling errors.
  197. def get(self):
  198. with self._rlock:
  199. res = self._reader.recv_bytes()
  200. # unserialize the data after having released the lock
  201. return loads(res)
  202. # Overload put to use our customizable reducer
  203. def put(self, obj):
  204. # serialize the data before acquiring the lock
  205. obj = dumps(obj, reducers=self._reducers)
  206. if self._wlock is None:
  207. # writes to a message oriented win32 pipe are atomic
  208. self._writer.send_bytes(obj)
  209. else:
  210. with self._wlock:
  211. self._writer.send_bytes(obj)