queue.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. # Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
  2. """Synchronized queues.
  3. The :mod:`gevent.queue` module implements multi-producer, multi-consumer queues
  4. that work across greenlets, with the API similar to the classes found in the
  5. standard :mod:`Queue` and :class:`multiprocessing <multiprocessing.Queue>` modules.
  6. The classes in this module implement iterator protocol. Iterating over queue
  7. means repeatedly calling :meth:`get <Queue.get>` until :meth:`get <Queue.get>` returns ``StopIteration``.
  8. >>> queue = gevent.queue.Queue()
  9. >>> queue.put(1)
  10. >>> queue.put(2)
  11. >>> queue.put(StopIteration)
  12. >>> for item in queue:
  13. ... print(item)
  14. 1
  15. 2
  16. .. versionchanged:: 1.0
  17. ``Queue(0)`` now means queue of infinite size, not a channel. A :exc:`DeprecationWarning`
  18. will be issued with this argument.
  19. """
  20. from __future__ import absolute_import
  21. import sys
  22. import heapq
  23. import collections
  24. if sys.version_info[0] == 2:
  25. import Queue as __queue__
  26. else:
  27. import queue as __queue__ # python 2: pylint:disable=import-error
  28. Full = __queue__.Full
  29. Empty = __queue__.Empty
  30. from gevent.timeout import Timeout
  31. from gevent.hub import get_hub, Waiter, getcurrent
  32. from gevent.hub import InvalidSwitchError
  33. __all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'JoinableQueue', 'Channel']
  34. def _safe_remove(deq, item):
  35. # For when the item may have been removed by
  36. # Queue._unlock
  37. try:
  38. deq.remove(item)
  39. except ValueError:
  40. pass
  41. class Queue(object):
  42. """
  43. Create a queue object with a given maximum size.
  44. If *maxsize* is less than or equal to zero or ``None``, the queue
  45. size is infinite.
  46. .. versionchanged:: 1.1b3
  47. Queues now support :func:`len`; it behaves the same as :meth:`qsize`.
  48. .. versionchanged:: 1.1b3
  49. Multiple greenlets that block on a call to :meth:`put` for a full queue
  50. will now be woken up to put their items into the queue in the order in which
  51. they arrived. Likewise, multiple greenlets that block on a call to :meth:`get` for
  52. an empty queue will now receive items in the order in which they blocked. An
  53. implementation quirk under CPython *usually* ensured this was roughly the case
  54. previously anyway, but that wasn't the case for PyPy.
  55. """
  56. def __init__(self, maxsize=None, items=None):
  57. if maxsize is not None and maxsize <= 0:
  58. self.maxsize = None
  59. if maxsize == 0:
  60. import warnings
  61. warnings.warn('Queue(0) now equivalent to Queue(None); if you want a channel, use Channel',
  62. DeprecationWarning, stacklevel=2)
  63. else:
  64. self.maxsize = maxsize
  65. # Explicitly maintain order for getters and putters that block
  66. # so that callers can consistently rely on getting things out
  67. # in the apparent order they went in. This was once required by
  68. # imap_unordered. Previously these were set() objects, and the
  69. # items put in the set have default hash() and eq() methods;
  70. # under CPython, since new objects tend to have increasing
  71. # hash values, this tended to roughly maintain order anyway,
  72. # but that's not true under PyPy. An alternative to a deque
  73. # (to avoid the linear scan of remove()) might be an
  74. # OrderedDict, but it's 2.7 only; we don't expect to have so
  75. # many waiters that removing an arbitrary element is a
  76. # bottleneck, though.
  77. self.getters = collections.deque()
  78. self.putters = collections.deque()
  79. self.hub = get_hub()
  80. self._event_unlock = None
  81. if items:
  82. self._init(maxsize, items)
  83. else:
  84. self._init(maxsize)
  85. # QQQ make maxsize into a property with setter that schedules unlock if necessary
  86. def copy(self):
  87. return type(self)(self.maxsize, self.queue)
  88. def _init(self, maxsize, items=None):
  89. # FIXME: Why is maxsize unused or even passed?
  90. # pylint:disable=unused-argument
  91. if items:
  92. self.queue = collections.deque(items)
  93. else:
  94. self.queue = collections.deque()
  95. def _get(self):
  96. return self.queue.popleft()
  97. def _peek(self):
  98. return self.queue[0]
  99. def _put(self, item):
  100. self.queue.append(item)
  101. def __repr__(self):
  102. return '<%s at %s%s>' % (type(self).__name__, hex(id(self)), self._format())
  103. def __str__(self):
  104. return '<%s%s>' % (type(self).__name__, self._format())
  105. def _format(self):
  106. result = []
  107. if self.maxsize is not None:
  108. result.append('maxsize=%r' % (self.maxsize, ))
  109. if getattr(self, 'queue', None):
  110. result.append('queue=%r' % (self.queue, ))
  111. if self.getters:
  112. result.append('getters[%s]' % len(self.getters))
  113. if self.putters:
  114. result.append('putters[%s]' % len(self.putters))
  115. if result:
  116. return ' ' + ' '.join(result)
  117. return ''
  118. def qsize(self):
  119. """Return the size of the queue."""
  120. return len(self.queue)
  121. def __len__(self):
  122. """
  123. Return the size of the queue. This is the same as :meth:`qsize`.
  124. .. versionadded: 1.1b3
  125. Previously, getting len() of a queue would raise a TypeError.
  126. """
  127. return self.qsize()
  128. def __bool__(self):
  129. """
  130. A queue object is always True.
  131. .. versionadded: 1.1b3
  132. Now that queues support len(), they need to implement ``__bool__``
  133. to return True for backwards compatibility.
  134. """
  135. return True
  136. __nonzero__ = __bool__
  137. def empty(self):
  138. """Return ``True`` if the queue is empty, ``False`` otherwise."""
  139. return not self.qsize()
  140. def full(self):
  141. """Return ``True`` if the queue is full, ``False`` otherwise.
  142. ``Queue(None)`` is never full.
  143. """
  144. return self.maxsize is not None and self.qsize() >= self.maxsize
  145. def put(self, item, block=True, timeout=None):
  146. """Put an item into the queue.
  147. If optional arg *block* is true and *timeout* is ``None`` (the default),
  148. block if necessary until a free slot is available. If *timeout* is
  149. a positive number, it blocks at most *timeout* seconds and raises
  150. the :class:`Full` exception if no free slot was available within that time.
  151. Otherwise (*block* is false), put an item on the queue if a free slot
  152. is immediately available, else raise the :class:`Full` exception (*timeout*
  153. is ignored in that case).
  154. """
  155. if self.maxsize is None or self.qsize() < self.maxsize:
  156. # there's a free slot, put an item right away
  157. self._put(item)
  158. if self.getters:
  159. self._schedule_unlock()
  160. elif self.hub is getcurrent():
  161. # We're in the mainloop, so we cannot wait; we can switch to other greenlets though.
  162. # Check if possible to get a free slot in the queue.
  163. while self.getters and self.qsize() and self.qsize() >= self.maxsize:
  164. getter = self.getters.popleft()
  165. getter.switch(getter)
  166. if self.qsize() < self.maxsize:
  167. self._put(item)
  168. return
  169. raise Full
  170. elif block:
  171. waiter = ItemWaiter(item, self)
  172. self.putters.append(waiter)
  173. timeout = Timeout._start_new_or_dummy(timeout, Full)
  174. try:
  175. if self.getters:
  176. self._schedule_unlock()
  177. result = waiter.get()
  178. if result is not waiter:
  179. raise InvalidSwitchError("Invalid switch into Queue.put: %r" % (result, ))
  180. finally:
  181. timeout.cancel()
  182. _safe_remove(self.putters, waiter)
  183. else:
  184. raise Full
  185. def put_nowait(self, item):
  186. """Put an item into the queue without blocking.
  187. Only enqueue the item if a free slot is immediately available.
  188. Otherwise raise the :class:`Full` exception.
  189. """
  190. self.put(item, False)
  191. def __get_or_peek(self, method, block, timeout):
  192. # Internal helper method. The `method` should be either
  193. # self._get when called from self.get() or self._peek when
  194. # called from self.peek(). Call this after the initial check
  195. # to see if there are items in the queue.
  196. if self.hub is getcurrent():
  197. # special case to make get_nowait() or peek_nowait() runnable in the mainloop greenlet
  198. # there are no items in the queue; try to fix the situation by unlocking putters
  199. while self.putters:
  200. # Note: get() used popleft(), peek used pop(); popleft
  201. # is almost certainly correct.
  202. self.putters.popleft().put_and_switch()
  203. if self.qsize():
  204. return method()
  205. raise Empty()
  206. if not block:
  207. # We can't block, we're not the hub, and we have nothing
  208. # to return. No choice...
  209. raise Empty()
  210. waiter = Waiter()
  211. timeout = Timeout._start_new_or_dummy(timeout, Empty)
  212. try:
  213. self.getters.append(waiter)
  214. if self.putters:
  215. self._schedule_unlock()
  216. result = waiter.get()
  217. if result is not waiter:
  218. raise InvalidSwitchError('Invalid switch into Queue.get: %r' % (result, ))
  219. return method()
  220. finally:
  221. timeout.cancel()
  222. _safe_remove(self.getters, waiter)
  223. def get(self, block=True, timeout=None):
  224. """Remove and return an item from the queue.
  225. If optional args *block* is true and *timeout* is ``None`` (the default),
  226. block if necessary until an item is available. If *timeout* is a positive number,
  227. it blocks at most *timeout* seconds and raises the :class:`Empty` exception
  228. if no item was available within that time. Otherwise (*block* is false), return
  229. an item if one is immediately available, else raise the :class:`Empty` exception
  230. (*timeout* is ignored in that case).
  231. """
  232. if self.qsize():
  233. if self.putters:
  234. self._schedule_unlock()
  235. return self._get()
  236. return self.__get_or_peek(self._get, block, timeout)
  237. def get_nowait(self):
  238. """Remove and return an item from the queue without blocking.
  239. Only get an item if one is immediately available. Otherwise
  240. raise the :class:`Empty` exception.
  241. """
  242. return self.get(False)
  243. def peek(self, block=True, timeout=None):
  244. """Return an item from the queue without removing it.
  245. If optional args *block* is true and *timeout* is ``None`` (the default),
  246. block if necessary until an item is available. If *timeout* is a positive number,
  247. it blocks at most *timeout* seconds and raises the :class:`Empty` exception
  248. if no item was available within that time. Otherwise (*block* is false), return
  249. an item if one is immediately available, else raise the :class:`Empty` exception
  250. (*timeout* is ignored in that case).
  251. """
  252. if self.qsize():
  253. # XXX: Why doesn't this schedule an unlock like get() does?
  254. return self._peek()
  255. return self.__get_or_peek(self._peek, block, timeout)
  256. def peek_nowait(self):
  257. """Return an item from the queue without blocking.
  258. Only return an item if one is immediately available. Otherwise
  259. raise the :class:`Empty` exception.
  260. """
  261. return self.peek(False)
  262. def _unlock(self):
  263. while True:
  264. repeat = False
  265. if self.putters and (self.maxsize is None or self.qsize() < self.maxsize):
  266. repeat = True
  267. try:
  268. putter = self.putters.popleft()
  269. self._put(putter.item)
  270. except: # pylint:disable=bare-except
  271. putter.throw(*sys.exc_info())
  272. else:
  273. putter.switch(putter)
  274. if self.getters and self.qsize():
  275. repeat = True
  276. getter = self.getters.popleft()
  277. getter.switch(getter)
  278. if not repeat:
  279. return
  280. def _schedule_unlock(self):
  281. if not self._event_unlock:
  282. self._event_unlock = self.hub.loop.run_callback(self._unlock)
  283. def __iter__(self):
  284. return self
  285. def next(self):
  286. result = self.get()
  287. if result is StopIteration:
  288. raise result
  289. return result
  290. __next__ = next
  291. class ItemWaiter(Waiter):
  292. __slots__ = ['item', 'queue']
  293. def __init__(self, item, queue):
  294. Waiter.__init__(self)
  295. self.item = item
  296. self.queue = queue
  297. def put_and_switch(self):
  298. self.queue._put(self.item)
  299. self.queue = None
  300. self.item = None
  301. return self.switch(self)
  302. class PriorityQueue(Queue):
  303. '''A subclass of :class:`Queue` that retrieves entries in priority order (lowest first).
  304. Entries are typically tuples of the form: ``(priority number, data)``.
  305. .. versionchanged:: 1.2a1
  306. Any *items* given to the constructor will now be passed through
  307. :func:`heapq.heapify` to ensure the invariants of this class hold.
  308. Previously it was just assumed that they were already a heap.
  309. '''
  310. def _init(self, maxsize, items=None):
  311. if items:
  312. self.queue = list(items)
  313. heapq.heapify(self.queue)
  314. else:
  315. self.queue = []
  316. def _put(self, item, heappush=heapq.heappush):
  317. # pylint:disable=arguments-differ
  318. heappush(self.queue, item)
  319. def _get(self, heappop=heapq.heappop):
  320. # pylint:disable=arguments-differ
  321. return heappop(self.queue)
  322. class LifoQueue(Queue):
  323. '''A subclass of :class:`Queue` that retrieves most recently added entries first.'''
  324. def _init(self, maxsize, items=None):
  325. if items:
  326. self.queue = list(items)
  327. else:
  328. self.queue = []
  329. def _put(self, item):
  330. self.queue.append(item)
  331. def _get(self):
  332. return self.queue.pop()
  333. def _peek(self):
  334. return self.queue[-1]
  335. class JoinableQueue(Queue):
  336. """
  337. A subclass of :class:`Queue` that additionally has
  338. :meth:`task_done` and :meth:`join` methods.
  339. """
  340. def __init__(self, maxsize=None, items=None, unfinished_tasks=None):
  341. """
  342. .. versionchanged:: 1.1a1
  343. If *unfinished_tasks* is not given, then all the given *items*
  344. (if any) will be considered unfinished.
  345. """
  346. from gevent.event import Event
  347. Queue.__init__(self, maxsize, items)
  348. self._cond = Event()
  349. self._cond.set()
  350. if unfinished_tasks:
  351. self.unfinished_tasks = unfinished_tasks
  352. elif items:
  353. self.unfinished_tasks = len(items)
  354. else:
  355. self.unfinished_tasks = 0
  356. if self.unfinished_tasks:
  357. self._cond.clear()
  358. def copy(self):
  359. return type(self)(self.maxsize, self.queue, self.unfinished_tasks)
  360. def _format(self):
  361. result = Queue._format(self)
  362. if self.unfinished_tasks:
  363. result += ' tasks=%s _cond=%s' % (self.unfinished_tasks, self._cond)
  364. return result
  365. def _put(self, item):
  366. Queue._put(self, item)
  367. self.unfinished_tasks += 1
  368. self._cond.clear()
  369. def task_done(self):
  370. '''Indicate that a formerly enqueued task is complete. Used by queue consumer threads.
  371. For each :meth:`get <Queue.get>` used to fetch a task, a subsequent call to :meth:`task_done` tells the queue
  372. that the processing on the task is complete.
  373. If a :meth:`join` is currently blocking, it will resume when all items have been processed
  374. (meaning that a :meth:`task_done` call was received for every item that had been
  375. :meth:`put <Queue.put>` into the queue).
  376. Raises a :exc:`ValueError` if called more times than there were items placed in the queue.
  377. '''
  378. if self.unfinished_tasks <= 0:
  379. raise ValueError('task_done() called too many times')
  380. self.unfinished_tasks -= 1
  381. if self.unfinished_tasks == 0:
  382. self._cond.set()
  383. def join(self, timeout=None):
  384. '''
  385. Block until all items in the queue have been gotten and processed.
  386. The count of unfinished tasks goes up whenever an item is added to the queue.
  387. The count goes down whenever a consumer thread calls :meth:`task_done` to indicate
  388. that the item was retrieved and all work on it is complete. When the count of
  389. unfinished tasks drops to zero, :meth:`join` unblocks.
  390. :param float timeout: If not ``None``, then wait no more than this time in seconds
  391. for all tasks to finish.
  392. :return: ``True`` if all tasks have finished; if ``timeout`` was given and expired before
  393. all tasks finished, ``False``.
  394. .. versionchanged:: 1.1a1
  395. Add the *timeout* parameter.
  396. '''
  397. return self._cond.wait(timeout=timeout)
  398. class Channel(object):
  399. def __init__(self):
  400. self.getters = collections.deque()
  401. self.putters = collections.deque()
  402. self.hub = get_hub()
  403. self._event_unlock = None
  404. def __repr__(self):
  405. return '<%s at %s %s>' % (type(self).__name__, hex(id(self)), self._format())
  406. def __str__(self):
  407. return '<%s %s>' % (type(self).__name__, self._format())
  408. def _format(self):
  409. result = ''
  410. if self.getters:
  411. result += ' getters[%s]' % len(self.getters)
  412. if self.putters:
  413. result += ' putters[%s]' % len(self.putters)
  414. return result
  415. @property
  416. def balance(self):
  417. return len(self.putters) - len(self.getters)
  418. def qsize(self):
  419. return 0
  420. def empty(self):
  421. return True
  422. def full(self):
  423. return True
  424. def put(self, item, block=True, timeout=None):
  425. if self.hub is getcurrent():
  426. if self.getters:
  427. getter = self.getters.popleft()
  428. getter.switch(item)
  429. return
  430. raise Full
  431. if not block:
  432. timeout = 0
  433. waiter = Waiter()
  434. item = (item, waiter)
  435. self.putters.append(item)
  436. timeout = Timeout._start_new_or_dummy(timeout, Full)
  437. try:
  438. if self.getters:
  439. self._schedule_unlock()
  440. result = waiter.get()
  441. if result is not waiter:
  442. raise InvalidSwitchError("Invalid switch into Channel.put: %r" % (result, ))
  443. except:
  444. _safe_remove(self.putters, item)
  445. raise
  446. finally:
  447. timeout.cancel()
  448. def put_nowait(self, item):
  449. self.put(item, False)
  450. def get(self, block=True, timeout=None):
  451. if self.hub is getcurrent():
  452. if self.putters:
  453. item, putter = self.putters.popleft()
  454. self.hub.loop.run_callback(putter.switch, putter)
  455. return item
  456. if not block:
  457. timeout = 0
  458. waiter = Waiter()
  459. timeout = Timeout._start_new_or_dummy(timeout, Empty)
  460. try:
  461. self.getters.append(waiter)
  462. if self.putters:
  463. self._schedule_unlock()
  464. return waiter.get()
  465. except:
  466. self.getters.remove(waiter)
  467. raise
  468. finally:
  469. timeout.cancel()
  470. def get_nowait(self):
  471. return self.get(False)
  472. def _unlock(self):
  473. while self.putters and self.getters:
  474. getter = self.getters.popleft()
  475. item, putter = self.putters.popleft()
  476. getter.switch(item)
  477. putter.switch(putter)
  478. def _schedule_unlock(self):
  479. if not self._event_unlock:
  480. self._event_unlock = self.hub.loop.run_callback(self._unlock)
  481. def __iter__(self):
  482. return self
  483. def next(self):
  484. result = self.get()
  485. if result is StopIteration:
  486. raise result
  487. return result
  488. __next__ = next # py3