queues.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 _terminate_broken(self):
  136. # Close a Queue on error.
  137. # gh-94777: Prevent queue writing to a pipe which is no longer read.
  138. self._reader.close()
  139. # gh-107219: Close the connection writer which can unblock
  140. # Queue._feed() if it was stuck in send_bytes().
  141. if sys.platform == 'win32':
  142. self._writer.close()
  143. self.close()
  144. self.join_thread()
  145. def _start_thread(self):
  146. debug('Queue._start_thread()')
  147. # Start thread which transfers data from buffer to pipe
  148. self._buffer.clear()
  149. self._thread = threading.Thread(
  150. target=Queue._feed,
  151. args=(self._buffer, self._notempty, self._send_bytes,
  152. self._wlock, self._reader.close, self._writer.close,
  153. self._ignore_epipe, self._on_queue_feeder_error,
  154. self._sem),
  155. name='QueueFeederThread',
  156. daemon=True,
  157. )
  158. try:
  159. debug('doing self._thread.start()')
  160. self._thread.start()
  161. debug('... done self._thread.start()')
  162. except:
  163. # gh-109047: During Python finalization, creating a thread
  164. # can fail with RuntimeError.
  165. self._thread = None
  166. raise
  167. if not self._joincancelled:
  168. self._jointhread = Finalize(
  169. self._thread, Queue._finalize_join,
  170. [weakref.ref(self._thread)],
  171. exitpriority=-5
  172. )
  173. # Send sentinel to the thread queue object when garbage collected
  174. self._close = Finalize(
  175. self, Queue._finalize_close,
  176. [self._buffer, self._notempty],
  177. exitpriority=10
  178. )
  179. @staticmethod
  180. def _finalize_join(twr):
  181. debug('joining queue thread')
  182. thread = twr()
  183. if thread is not None:
  184. thread.join()
  185. debug('... queue thread joined')
  186. else:
  187. debug('... queue thread already dead')
  188. @staticmethod
  189. def _finalize_close(buffer, notempty):
  190. debug('telling queue thread to quit')
  191. with notempty:
  192. buffer.append(_sentinel)
  193. notempty.notify()
  194. @staticmethod
  195. def _feed(buffer, notempty, send_bytes, writelock, reader_close,
  196. writer_close, ignore_epipe, onerror, queue_sem):
  197. debug('starting thread to feed data to pipe')
  198. nacquire = notempty.acquire
  199. nrelease = notempty.release
  200. nwait = notempty.wait
  201. bpopleft = buffer.popleft
  202. sentinel = _sentinel
  203. if sys.platform != 'win32':
  204. wacquire = writelock.acquire
  205. wrelease = writelock.release
  206. else:
  207. wacquire = None
  208. while 1:
  209. try:
  210. nacquire()
  211. try:
  212. if not buffer:
  213. nwait()
  214. finally:
  215. nrelease()
  216. try:
  217. while 1:
  218. obj = bpopleft()
  219. if obj is sentinel:
  220. debug('feeder thread got sentinel -- exiting')
  221. reader_close()
  222. writer_close()
  223. return
  224. # serialize the data before acquiring the lock
  225. obj = _ForkingPickler.dumps(obj)
  226. if wacquire is None:
  227. send_bytes(obj)
  228. else:
  229. wacquire()
  230. try:
  231. send_bytes(obj)
  232. finally:
  233. wrelease()
  234. except IndexError:
  235. pass
  236. except Exception as e:
  237. if ignore_epipe and getattr(e, 'errno', 0) == errno.EPIPE:
  238. return
  239. # Since this runs in a daemon thread the resources it uses
  240. # may be become unusable while the process is cleaning up.
  241. # We ignore errors which happen after the process has
  242. # started to cleanup.
  243. if is_exiting():
  244. info('error in queue thread: %s', e)
  245. return
  246. else:
  247. # Since the object has not been sent in the queue, we need
  248. # to decrease the size of the queue. The error acts as
  249. # if the object had been silently removed from the queue
  250. # and this step is necessary to have a properly working
  251. # queue.
  252. queue_sem.release()
  253. onerror(e, obj)
  254. @staticmethod
  255. def _on_queue_feeder_error(e, obj):
  256. """
  257. Private API hook called when feeding data in the background thread
  258. raises an exception. For overriding by concurrent.futures.
  259. """
  260. import traceback
  261. traceback.print_exc()
  262. __class_getitem__ = classmethod(types.GenericAlias)
  263. _sentinel = object()
  264. #
  265. # A queue type which also supports join() and task_done() methods
  266. #
  267. # Note that if you do not call task_done() for each finished task then
  268. # eventually the counter's semaphore may overflow causing Bad Things
  269. # to happen.
  270. #
  271. class JoinableQueue(Queue):
  272. def __init__(self, maxsize=0, *, ctx):
  273. Queue.__init__(self, maxsize, ctx=ctx)
  274. self._unfinished_tasks = ctx.Semaphore(0)
  275. self._cond = ctx.Condition()
  276. def __getstate__(self):
  277. return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)
  278. def __setstate__(self, state):
  279. Queue.__setstate__(self, state[:-2])
  280. self._cond, self._unfinished_tasks = state[-2:]
  281. def put(self, obj, block=True, timeout=None):
  282. if self._closed:
  283. raise ValueError(f"Queue {self!r} is closed")
  284. if not self._sem.acquire(block, timeout):
  285. raise Full
  286. with self._notempty, self._cond:
  287. if self._thread is None:
  288. self._start_thread()
  289. self._buffer.append(obj)
  290. self._unfinished_tasks.release()
  291. self._notempty.notify()
  292. def task_done(self):
  293. with self._cond:
  294. if not self._unfinished_tasks.acquire(False):
  295. raise ValueError('task_done() called too many times')
  296. if self._unfinished_tasks._semlock._is_zero():
  297. self._cond.notify_all()
  298. def join(self):
  299. with self._cond:
  300. if not self._unfinished_tasks._semlock._is_zero():
  301. self._cond.wait()
  302. #
  303. # Simplified Queue type -- really just a locked pipe
  304. #
  305. class SimpleQueue(object):
  306. def __init__(self, *, ctx):
  307. self._reader, self._writer = connection.Pipe(duplex=False)
  308. self._rlock = ctx.Lock()
  309. self._poll = self._reader.poll
  310. if sys.platform == 'win32':
  311. self._wlock = None
  312. else:
  313. self._wlock = ctx.Lock()
  314. def close(self):
  315. self._reader.close()
  316. self._writer.close()
  317. def empty(self):
  318. return not self._poll()
  319. def __getstate__(self):
  320. context.assert_spawning(self)
  321. return (self._reader, self._writer, self._rlock, self._wlock)
  322. def __setstate__(self, state):
  323. (self._reader, self._writer, self._rlock, self._wlock) = state
  324. self._poll = self._reader.poll
  325. def get(self):
  326. with self._rlock:
  327. res = self._reader.recv_bytes()
  328. # unserialize the data after having released the lock
  329. return _ForkingPickler.loads(res)
  330. def put(self, obj):
  331. # serialize the data before acquiring the lock
  332. obj = _ForkingPickler.dumps(obj)
  333. if self._wlock is None:
  334. # writes to a message oriented win32 pipe are atomic
  335. self._writer.send_bytes(obj)
  336. else:
  337. with self._wlock:
  338. self._writer.send_bytes(obj)
  339. __class_getitem__ = classmethod(types.GenericAlias)