queues.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #
  2. # Module implementing queues
  3. #
  4. # multiprocessing/queues.py
  5. #
  6. # Copyright (c) 2006-2008, R Oudkerk
  7. # Licensed to PSF under a Contributor Agreement.
  8. #
  9. __all__ = ['Queue', 'SimpleQueue', 'JoinableQueue']
  10. import sys
  11. import os
  12. import threading
  13. import collections
  14. import time
  15. import types
  16. import weakref
  17. import errno
  18. from queue import Empty, Full
  19. import _multiprocessing
  20. from . import connection
  21. from . import context
  22. _ForkingPickler = context.reduction.ForkingPickler
  23. from .util import debug, info, Finalize, register_after_fork, is_exiting
  24. #
  25. # Queue type using a pipe, buffer and thread
  26. #
  27. class Queue(object):
  28. def __init__(self, maxsize=0, *, ctx):
  29. if maxsize <= 0:
  30. # Can raise ImportError (see issues #3770 and #23400)
  31. from .synchronize import SEM_VALUE_MAX as maxsize
  32. self._maxsize = maxsize
  33. self._reader, self._writer = connection.Pipe(duplex=False)
  34. self._rlock = ctx.Lock()
  35. self._opid = os.getpid()
  36. if sys.platform == 'win32':
  37. self._wlock = None
  38. else:
  39. self._wlock = ctx.Lock()
  40. self._sem = ctx.BoundedSemaphore(maxsize)
  41. # For use by concurrent.futures
  42. self._ignore_epipe = False
  43. self._reset()
  44. if sys.platform != 'win32':
  45. register_after_fork(self, Queue._after_fork)
  46. def __getstate__(self):
  47. context.assert_spawning(self)
  48. return (self._ignore_epipe, self._maxsize, self._reader, self._writer,
  49. self._rlock, self._wlock, self._sem, self._opid)
  50. def __setstate__(self, state):
  51. (self._ignore_epipe, self._maxsize, self._reader, self._writer,
  52. self._rlock, self._wlock, self._sem, self._opid) = state
  53. self._reset()
  54. def _after_fork(self):
  55. debug('Queue._after_fork()')
  56. self._reset(after_fork=True)
  57. def _reset(self, after_fork=False):
  58. if after_fork:
  59. self._notempty._at_fork_reinit()
  60. else:
  61. self._notempty = threading.Condition(threading.Lock())
  62. self._buffer = collections.deque()
  63. self._thread = None
  64. self._jointhread = None
  65. self._joincancelled = False
  66. self._closed = False
  67. self._close = None
  68. self._send_bytes = self._writer.send_bytes
  69. self._recv_bytes = self._reader.recv_bytes
  70. self._poll = self._reader.poll
  71. def put(self, obj, block=True, timeout=None):
  72. if self._closed:
  73. raise ValueError(f"Queue {self!r} is closed")
  74. if not self._sem.acquire(block, timeout):
  75. raise Full
  76. with self._notempty:
  77. if self._thread is None:
  78. self._start_thread()
  79. self._buffer.append(obj)
  80. self._notempty.notify()
  81. def get(self, block=True, timeout=None):
  82. if self._closed:
  83. raise ValueError(f"Queue {self!r} is closed")
  84. if block and timeout is None:
  85. with self._rlock:
  86. res = self._recv_bytes()
  87. self._sem.release()
  88. else:
  89. if block:
  90. deadline = time.monotonic() + timeout
  91. if not self._rlock.acquire(block, timeout):
  92. raise Empty
  93. try:
  94. if block:
  95. timeout = deadline - time.monotonic()
  96. if not self._poll(timeout):
  97. raise Empty
  98. elif not self._poll():
  99. raise Empty
  100. res = self._recv_bytes()
  101. self._sem.release()
  102. finally:
  103. self._rlock.release()
  104. # unserialize the data after having released the lock
  105. return _ForkingPickler.loads(res)
  106. def qsize(self):
  107. # Raises NotImplementedError on Mac OSX because of broken sem_getvalue()
  108. return self._maxsize - self._sem._semlock._get_value()
  109. def empty(self):
  110. return not self._poll()
  111. def full(self):
  112. return self._sem._semlock._is_zero()
  113. def get_nowait(self):
  114. return self.get(False)
  115. def put_nowait(self, obj):
  116. return self.put(obj, False)
  117. def close(self):
  118. self._closed = True
  119. close = self._close
  120. if close:
  121. self._close = None
  122. close()
  123. def join_thread(self):
  124. debug('Queue.join_thread()')
  125. assert self._closed, "Queue {0!r} not closed".format(self)
  126. if self._jointhread:
  127. self._jointhread()
  128. def cancel_join_thread(self):
  129. debug('Queue.cancel_join_thread()')
  130. self._joincancelled = True
  131. try:
  132. self._jointhread.cancel()
  133. except AttributeError:
  134. pass
  135. def _start_thread(self):
  136. debug('Queue._start_thread()')
  137. # Start thread which transfers data from buffer to pipe
  138. self._buffer.clear()
  139. self._thread = threading.Thread(
  140. target=Queue._feed,
  141. args=(self._buffer, self._notempty, self._send_bytes,
  142. self._wlock, self._reader.close, self._writer.close,
  143. self._ignore_epipe, self._on_queue_feeder_error,
  144. self._sem),
  145. name='QueueFeederThread'
  146. )
  147. self._thread.daemon = True
  148. debug('doing self._thread.start()')
  149. self._thread.start()
  150. debug('... done self._thread.start()')
  151. if not self._joincancelled:
  152. self._jointhread = Finalize(
  153. self._thread, Queue._finalize_join,
  154. [weakref.ref(self._thread)],
  155. exitpriority=-5
  156. )
  157. # Send sentinel to the thread queue object when garbage collected
  158. self._close = Finalize(
  159. self, Queue._finalize_close,
  160. [self._buffer, self._notempty],
  161. exitpriority=10
  162. )
  163. @staticmethod
  164. def _finalize_join(twr):
  165. debug('joining queue thread')
  166. thread = twr()
  167. if thread is not None:
  168. thread.join()
  169. debug('... queue thread joined')
  170. else:
  171. debug('... queue thread already dead')
  172. @staticmethod
  173. def _finalize_close(buffer, notempty):
  174. debug('telling queue thread to quit')
  175. with notempty:
  176. buffer.append(_sentinel)
  177. notempty.notify()
  178. @staticmethod
  179. def _feed(buffer, notempty, send_bytes, writelock, reader_close,
  180. writer_close, ignore_epipe, onerror, queue_sem):
  181. debug('starting thread to feed data to pipe')
  182. nacquire = notempty.acquire
  183. nrelease = notempty.release
  184. nwait = notempty.wait
  185. bpopleft = buffer.popleft
  186. sentinel = _sentinel
  187. if sys.platform != 'win32':
  188. wacquire = writelock.acquire
  189. wrelease = writelock.release
  190. else:
  191. wacquire = None
  192. while 1:
  193. try:
  194. nacquire()
  195. try:
  196. if not buffer:
  197. nwait()
  198. finally:
  199. nrelease()
  200. try:
  201. while 1:
  202. obj = bpopleft()
  203. if obj is sentinel:
  204. debug('feeder thread got sentinel -- exiting')
  205. reader_close()
  206. writer_close()
  207. return
  208. # serialize the data before acquiring the lock
  209. obj = _ForkingPickler.dumps(obj)
  210. if wacquire is None:
  211. send_bytes(obj)
  212. else:
  213. wacquire()
  214. try:
  215. send_bytes(obj)
  216. finally:
  217. wrelease()
  218. except IndexError:
  219. pass
  220. except Exception as e:
  221. if ignore_epipe and getattr(e, 'errno', 0) == errno.EPIPE:
  222. return
  223. # Since this runs in a daemon thread the resources it uses
  224. # may be become unusable while the process is cleaning up.
  225. # We ignore errors which happen after the process has
  226. # started to cleanup.
  227. if is_exiting():
  228. info('error in queue thread: %s', e)
  229. return
  230. else:
  231. # Since the object has not been sent in the queue, we need
  232. # to decrease the size of the queue. The error acts as
  233. # if the object had been silently removed from the queue
  234. # and this step is necessary to have a properly working
  235. # queue.
  236. queue_sem.release()
  237. onerror(e, obj)
  238. @staticmethod
  239. def _on_queue_feeder_error(e, obj):
  240. """
  241. Private API hook called when feeding data in the background thread
  242. raises an exception. For overriding by concurrent.futures.
  243. """
  244. import traceback
  245. traceback.print_exc()
  246. __class_getitem__ = classmethod(types.GenericAlias)
  247. _sentinel = object()
  248. #
  249. # A queue type which also supports join() and task_done() methods
  250. #
  251. # Note that if you do not call task_done() for each finished task then
  252. # eventually the counter's semaphore may overflow causing Bad Things
  253. # to happen.
  254. #
  255. class JoinableQueue(Queue):
  256. def __init__(self, maxsize=0, *, ctx):
  257. Queue.__init__(self, maxsize, ctx=ctx)
  258. self._unfinished_tasks = ctx.Semaphore(0)
  259. self._cond = ctx.Condition()
  260. def __getstate__(self):
  261. return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)
  262. def __setstate__(self, state):
  263. Queue.__setstate__(self, state[:-2])
  264. self._cond, self._unfinished_tasks = state[-2:]
  265. def put(self, obj, block=True, timeout=None):
  266. if self._closed:
  267. raise ValueError(f"Queue {self!r} is closed")
  268. if not self._sem.acquire(block, timeout):
  269. raise Full
  270. with self._notempty, self._cond:
  271. if self._thread is None:
  272. self._start_thread()
  273. self._buffer.append(obj)
  274. self._unfinished_tasks.release()
  275. self._notempty.notify()
  276. def task_done(self):
  277. with self._cond:
  278. if not self._unfinished_tasks.acquire(False):
  279. raise ValueError('task_done() called too many times')
  280. if self._unfinished_tasks._semlock._is_zero():
  281. self._cond.notify_all()
  282. def join(self):
  283. with self._cond:
  284. if not self._unfinished_tasks._semlock._is_zero():
  285. self._cond.wait()
  286. #
  287. # Simplified Queue type -- really just a locked pipe
  288. #
  289. class SimpleQueue(object):
  290. def __init__(self, *, ctx):
  291. self._reader, self._writer = connection.Pipe(duplex=False)
  292. self._rlock = ctx.Lock()
  293. self._poll = self._reader.poll
  294. if sys.platform == 'win32':
  295. self._wlock = None
  296. else:
  297. self._wlock = ctx.Lock()
  298. def close(self):
  299. self._reader.close()
  300. self._writer.close()
  301. def empty(self):
  302. return not self._poll()
  303. def __getstate__(self):
  304. context.assert_spawning(self)
  305. return (self._reader, self._writer, self._rlock, self._wlock)
  306. def __setstate__(self, state):
  307. (self._reader, self._writer, self._rlock, self._wlock) = state
  308. self._poll = self._reader.poll
  309. def get(self):
  310. with self._rlock:
  311. res = self._reader.recv_bytes()
  312. # unserialize the data after having released the lock
  313. return _ForkingPickler.loads(res)
  314. def put(self, obj):
  315. # serialize the data before acquiring the lock
  316. obj = _ForkingPickler.dumps(obj)
  317. if self._wlock is None:
  318. # writes to a message oriented win32 pipe are atomic
  319. self._writer.send_bytes(obj)
  320. else:
  321. with self._wlock:
  322. self._writer.send_bytes(obj)
  323. __class_getitem__ = classmethod(types.GenericAlias)