Mercurial > cpython-lru_cache-weakref
annotate Lib/Queue.py @ 16413:8819867f14ef legacy-trunk
Patch derived from Trent's 101162: a Python/C API testing framework.
STILL NEEDS UNIX BUILD CHANGES.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Fri, 02 Feb 2001 05:57:15 +0000 |
| parents | b5345e99596b |
| children | 79d533934ac1 |
| 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 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15902
diff
changeset
|
3 __all__ = ["Queue","Empty","Full"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15902
diff
changeset
|
4 |
|
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
|
5 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
|
6 "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
|
7 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
|
8 |
|
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 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
|
10 "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
|
11 pass |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 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
|
14 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
|
15 """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
|
16 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
17 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
|
18 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
19 import thread |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
20 self._init(maxsize) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
21 self.mutex = thread.allocate_lock() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
22 self.esema = thread.allocate_lock() |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
23 self.esema.acquire() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
24 self.fsema = thread.allocate_lock() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
26 def qsize(self): |
| 10120 | 27 """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
|
28 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
|
29 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
|
30 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
|
31 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
33 def empty(self): |
| 10120 | 34 """Return 1 if the queue is empty, 0 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
|
35 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
|
36 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
|
37 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
|
38 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
40 def full(self): |
| 10120 | 41 """Return 1 if the queue is full, 0 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
|
42 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
|
43 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
|
44 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
|
45 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
| 10120 | 47 def put(self, item, block=1): |
|
7926
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
48 """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
|
49 |
| 10120 | 50 If optional arg 'block' is 1 (the default), block if |
| 51 necessary until a free slot is available. Otherwise (block | |
| 52 is 0), put an item on the queue if a free slot is immediately | |
| 53 available, else raise the Full exception. | |
| 54 """ | |
| 55 if block: | |
| 56 self.fsema.acquire() | |
| 57 elif not self.fsema.acquire(0): | |
| 58 raise Full | |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
59 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
|
60 was_empty = self._empty() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
61 self._put(item) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
62 if was_empty: |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
63 self.esema.release() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
64 if not 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
|
65 self.fsema.release() |
|
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
66 self.mutex.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
| 10120 | 68 def put_nowait(self, item): |
| 69 """Put an item into the queue without blocking. | |
| 70 | |
| 71 Only enqueue the item if a free slot is immediately available. | |
| 72 Otherwise raise the Full exception. | |
| 73 """ | |
| 74 return self.put(item, 0) | |
|
7926
e29d435ab142
Clarify that put *blocks* when the queue is full. Add some blank
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
75 |
| 10120 | 76 def get(self, block=1): |
| 77 """Remove and return an item from the queue. | |
| 78 | |
| 79 If optional arg 'block' is 1 (the default), block if | |
| 80 necessary until an item is available. Otherwise (block is 0), | |
| 81 return an item if one is immediately available, else raise the | |
| 82 Empty exception. | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
83 """ |
| 10120 | 84 if block: |
| 85 self.esema.acquire() | |
| 86 elif not self.esema.acquire(0): | |
| 87 raise Empty | |
|
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.acquire() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
89 was_full = self._full() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
90 item = self._get() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
91 if was_full: |
|
8197
3a55b4c5e3c7
Replace all calls to acquire_lock() and release_lock() with acquire()
Guido van Rossum <guido@python.org>
parents:
7926
diff
changeset
|
92 self.fsema.release() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
93 if not 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
|
94 self.esema.release() |
|
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 item |
|
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 get_nowait(self): |
| 10120 | 99 """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
|
100 |
| 10120 | 101 Only get an item if one is immediately available. Otherwise |
| 102 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
|
103 """ |
| 10120 | 104 return self.get(0) |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
105 |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
106 # 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
|
107 # (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
|
108 # 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
|
109 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
110 # 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
|
111 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
|
112 self.maxsize = maxsize |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
113 self.queue = [] |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
115 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
|
116 return len(self.queue) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
118 # 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
|
119 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
|
120 return not self.queue |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
122 # 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
|
123 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
|
124 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
|
125 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
126 # 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
|
127 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
|
128 self.queue.append(item) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
130 # 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
|
131 def _get(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
132 item = self.queue[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
133 del self.queue[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
134 return item |
