Mercurial > cpython-lru_cache-weakref
annotate Lib/Queue.py @ 44685:e511ccf2ffbe legacy-trunk
Tighten the definition of a named tuple.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Tue, 15 Jan 2008 03:07:42 +0000 |
| parents | 193146e4d276 |
| children | c790587f2992 787b969d37f0 |
| rev | line source |
|---|---|
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11267
diff
changeset
|
1 """A multi-producer, multi-consumer queue.""" |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
3 from time import time as _time |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
29711
diff
changeset
|
4 from collections import deque |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
5 |
| 29711 | 6 __all__ = ['Empty', 'Full', 'Queue'] |
| 7 | |
|
15902
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
8 class Empty(Exception): |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
9 "Exception raised by Queue.get(block=0)/get_nowait()." |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
10 pass |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
11 |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
12 class Full(Exception): |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
13 "Exception raised by Queue.put(block=0)/put_nowait()." |
|
eef321b6e41c
Variant of Skip's patch 103246 (Remove unneeded string exception compat from Queue).
Tim Peters <tim.peters@gmail.com>
parents:
13050
diff
changeset
|
14 pass |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 class Queue: |
|
38717
b23eeb802780
document the class, not its initializer
Skip Montanaro <skip@pobox.com>
parents:
37207
diff
changeset
|
17 """Create a queue object with a given maximum size. |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
38717
b23eeb802780
document the class, not its initializer
Skip Montanaro <skip@pobox.com>
parents:
37207
diff
changeset
|
19 If maxsize is <= 0, the queue size is infinite. |
|
b23eeb802780
document the class, not its initializer
Skip Montanaro <skip@pobox.com>
parents:
37207
diff
changeset
|
20 """ |
|
b23eeb802780
document the class, not its initializer
Skip Montanaro <skip@pobox.com>
parents:
37207
diff
changeset
|
21 def __init__(self, maxsize=0): |
|
26892
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
22 try: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
23 import threading |
|
26892
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
24 except ImportError: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
25 import dummy_threading as threading |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
26 self._init(maxsize) |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
27 # mutex must be held whenever the queue is mutating. All methods |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
28 # that acquire mutex must release it before returning. mutex |
|
40392
193146e4d276
... and the number of the counting shall be three.
Raymond Hettinger <python@rcn.com>
parents:
38717
diff
changeset
|
29 # is shared between the three conditions, so acquiring and |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
30 # releasing the conditions also acquires and releases mutex. |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
31 self.mutex = threading.Lock() |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
32 # Notify not_empty whenever an item is added to the queue; a |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
33 # thread waiting to get is notified then. |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
34 self.not_empty = threading.Condition(self.mutex) |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
35 # Notify not_full whenever an item is removed from the queue; |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
36 # a thread waiting to put is notified then. |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
37 self.not_full = threading.Condition(self.mutex) |
|
37198
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
38 # Notify all_tasks_done whenever the number of unfinished tasks |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
39 # drops to zero; thread waiting to join() is notified to resume |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
40 self.all_tasks_done = threading.Condition(self.mutex) |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
41 self.unfinished_tasks = 0 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
42 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
43 def task_done(self): |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
44 """Indicate that a formerly enqueued task is complete. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
45 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
46 Used by Queue consumer threads. For each get() used to fetch a task, |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
47 a subsequent call to task_done() tells the queue that the processing |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
48 on the task is complete. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
49 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
50 If a join() is currently blocking, it will resume when all items |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
51 have been processed (meaning that a task_done() call was received |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
52 for every item that had been put() into the queue). |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
53 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
54 Raises a ValueError if called more times than there were items |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
55 placed in the queue. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
56 """ |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
57 self.all_tasks_done.acquire() |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
58 try: |
|
37207
96e4d53128c9
Don't decrement below zero. And add more tests.
Raymond Hettinger <python@rcn.com>
parents:
37205
diff
changeset
|
59 unfinished = self.unfinished_tasks - 1 |
|
37198
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
60 if unfinished <= 0: |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
61 if unfinished < 0: |
|
37205
49622d65c29d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
37198
diff
changeset
|
62 raise ValueError('task_done() called too many times') |
|
37198
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
63 self.all_tasks_done.notifyAll() |
|
37207
96e4d53128c9
Don't decrement below zero. And add more tests.
Raymond Hettinger <python@rcn.com>
parents:
37205
diff
changeset
|
64 self.unfinished_tasks = unfinished |
|
37198
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
65 finally: |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
66 self.all_tasks_done.release() |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
67 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
68 def join(self): |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
69 """Blocks until all items in the Queue have been gotten and processed. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
70 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
71 The count of unfinished tasks goes up whenever an item is added to the |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
72 queue. The count goes down whenever a consumer thread calls task_done() |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
73 to indicate the item was retrieved and all work on it is complete. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
74 |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
75 When the count of unfinished tasks drops to zero, join() unblocks. |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
76 """ |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
77 self.all_tasks_done.acquire() |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
78 try: |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
79 while self.unfinished_tasks: |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
80 self.all_tasks_done.wait() |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
81 finally: |
|
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
82 self.all_tasks_done.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
84 def qsize(self): |
| 10120 | 85 """Return the approximate size of the queue (not reliable!).""" |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
86 self.mutex.acquire() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
87 n = self._qsize() |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
88 self.mutex.release() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
89 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
91 def empty(self): |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
92 """Return True if the queue is empty, False otherwise (not reliable!).""" |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
93 self.mutex.acquire() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
94 n = self._empty() |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
95 self.mutex.release() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
96 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
98 def full(self): |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
99 """Return True if the queue is full, False otherwise (not reliable!).""" |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
100 self.mutex.acquire() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
101 n = self._full() |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
102 self.mutex.release() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
103 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
105 def put(self, item, block=True, timeout=None): |
|
7926
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
106 """Put an item into the queue. |
|
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
107 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
108 If optional args 'block' is true and 'timeout' is None (the default), |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
109 block if necessary until a free slot is available. If 'timeout' is |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
110 a positive number, it blocks at most 'timeout' seconds and raises |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
111 the Full exception if no free slot was available within that time. |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
112 Otherwise ('block' is false), put an item on the queue if a free slot |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
113 is immediately available, else raise the Full exception ('timeout' |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
114 is ignored in that case). |
| 10120 | 115 """ |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
116 self.not_full.acquire() |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
117 try: |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
118 if not block: |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
119 if self._full(): |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
120 raise Full |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
121 elif timeout is None: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
122 while self._full(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
123 self.not_full.wait() |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
124 else: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
125 if timeout < 0: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
126 raise ValueError("'timeout' must be a positive number") |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
127 endtime = _time() + timeout |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
128 while self._full(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
129 remaining = endtime - _time() |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
130 if remaining <= 0.0: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
131 raise Full |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
132 self.not_full.wait(remaining) |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
133 self._put(item) |
|
37198
bed3f52f0dc6
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Raymond Hettinger <python@rcn.com>
parents:
32625
diff
changeset
|
134 self.unfinished_tasks += 1 |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
135 self.not_empty.notify() |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
136 finally: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
137 self.not_full.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
| 10120 | 139 def put_nowait(self, item): |
| 140 """Put an item into the queue without blocking. | |
| 141 | |
| 142 Only enqueue the item if a free slot is immediately available. | |
| 143 Otherwise raise the Full exception. | |
| 144 """ | |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
145 return self.put(item, False) |
|
7926
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
146 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
147 def get(self, block=True, timeout=None): |
| 10120 | 148 """Remove and return an item from the queue. |
| 149 | |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
150 If optional args 'block' is true and 'timeout' is None (the default), |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
151 block if necessary until an item is available. If 'timeout' is |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
152 a positive number, it blocks at most 'timeout' seconds and raises |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
153 the Empty exception if no item was available within that time. |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
154 Otherwise ('block' is false), return an item if one is immediately |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
155 available, else raise the Empty exception ('timeout' is ignored |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
156 in that case). |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
157 """ |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
158 self.not_empty.acquire() |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
159 try: |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
160 if not block: |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
161 if self._empty(): |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
162 raise Empty |
|
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
163 elif timeout is None: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
164 while self._empty(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
165 self.not_empty.wait() |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
166 else: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
167 if timeout < 0: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
168 raise ValueError("'timeout' must be a positive number") |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
169 endtime = _time() + timeout |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
170 while self._empty(): |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
171 remaining = endtime - _time() |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
172 if remaining <= 0.0: |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
173 raise Empty |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
174 self.not_empty.wait(remaining) |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
175 item = self._get() |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
176 self.not_full.notify() |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
177 return item |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
178 finally: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
179 self.not_empty.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
181 def get_nowait(self): |
| 10120 | 182 """Remove and return an item from the queue without blocking. |
|
7926
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
183 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
184 Only get an item if one is immediately available. Otherwise |
| 10120 | 185 raise the Empty exception. |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
186 """ |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
187 return self.get(False) |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
188 |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
189 # Override these methods to implement other queue organizations |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
190 # (e.g. stack or priority queue). |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
191 # These will only be called with appropriate locks held |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
193 # Initialize the queue representation |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
194 def _init(self, maxsize): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
195 self.maxsize = maxsize |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
29711
diff
changeset
|
196 self.queue = deque() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
198 def _qsize(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
199 return len(self.queue) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
201 # Check whether the queue is empty |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
202 def _empty(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
203 return not self.queue |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
205 # Check whether the queue is full |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
206 def _full(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
207 return self.maxsize > 0 and len(self.queue) == self.maxsize |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
209 # Put a new item in the queue |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
210 def _put(self, item): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
211 self.queue.append(item) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
213 # Get an item from the queue |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
214 def _get(self): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
29711
diff
changeset
|
215 return self.queue.popleft() |
