Mercurial > cpython-lru_cache-weakref
annotate Lib/Queue.py @ 32975:9b188158181f legacy-trunk
Typo fix; use 'in-place' instead of 'inplace'
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Fri, 06 Aug 2004 18:55:09 +0000 |
| parents | d42efc2b45e9 |
| children | f731859e9b3b bed3f52f0dc6 |
| 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: |
|
11267
2b26e48a85a6
Make the maxsize constructor argument default to 0 (an unlimited queue size).
Guido van Rossum <guido@python.org>
parents:
10120
diff
changeset
|
17 def __init__(self, maxsize=0): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
18 """Initialize 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
|
19 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
20 If maxsize is <= 0, the queue size is infinite. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
21 """ |
|
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 |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
29 # is shared between the two conditions, so acquiring and |
|
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) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
39 def qsize(self): |
| 10120 | 40 """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
|
41 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
|
42 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
|
43 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
|
44 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
46 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
|
47 """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
|
48 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
|
49 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
|
50 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
|
51 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
53 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
|
54 """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
|
55 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
|
56 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
|
57 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
|
58 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
60 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
|
61 """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
|
62 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 is ignored in that case). |
| 10120 | 70 """ |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 while self._full(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
78 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
|
79 else: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
80 if timeout < 0: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
81 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
|
82 endtime = _time() + timeout |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
83 while self._full(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
84 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
|
85 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
|
86 raise Full |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
87 self.not_full.wait(remaining) |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
88 self._put(item) |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
89 self.not_empty.notify() |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
90 finally: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
91 self.not_full.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
| 10120 | 93 def put_nowait(self, item): |
| 94 """Put an item into the queue without blocking. | |
| 95 | |
| 96 Only enqueue the item if a free slot is immediately available. | |
| 97 Otherwise raise the Full exception. | |
| 98 """ | |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
99 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
|
100 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
101 def get(self, block=True, timeout=None): |
| 10120 | 102 """Remove and return an item from the queue. |
| 103 | |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 """ |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 while self._empty(): |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
119 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
|
120 else: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
121 if timeout < 0: |
|
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 raise Empty |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
128 self.not_empty.wait(remaining) |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
129 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
|
130 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
|
131 return item |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
132 finally: |
|
32624
73016bd01a71
Bug #788520: Queue class has logic error when non-blocking
Tim Peters <tim.peters@gmail.com>
parents:
31436
diff
changeset
|
133 self.not_empty.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
135 def get_nowait(self): |
| 10120 | 136 """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
|
137 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
138 Only get an item if one is immediately available. Otherwise |
| 10120 | 139 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
|
140 """ |
|
32625
d42efc2b45e9
Simplified the new get/get_nowait/put/put_nowait implementations a bit.
Tim Peters <tim.peters@gmail.com>
parents:
32624
diff
changeset
|
141 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
|
142 |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
143 # 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
|
144 # (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
|
145 # 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
|
146 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
147 # 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
|
148 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
|
149 self.maxsize = maxsize |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
29711
diff
changeset
|
150 self.queue = deque() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
152 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
|
153 return len(self.queue) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
155 # 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
|
156 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
|
157 return not self.queue |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
159 # 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
|
160 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
|
161 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
|
162 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
163 # 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
|
164 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
|
165 self.queue.append(item) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
167 # 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
|
168 def _get(self): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
29711
diff
changeset
|
169 return self.queue.popleft() |
