Mercurial > cpython-lru_cache-weakref
annotate Lib/Queue.py @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | a195962e389c |
| children | eef321b6e41c |
| 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 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
3 # define this exception to be compatible with Python 1.5's class |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
4 # exceptions, but also when -X option is used. |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
5 try: |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
6 class Empty(Exception): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
7 pass |
| 10120 | 8 class Full(Exception): |
| 9 pass | |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
10 except TypeError: |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
11 # string based exceptions |
| 10120 | 12 # exception raised by get(block=0)/get_nowait() |
| 13 Empty = 'Queue.Empty' | |
| 14 # exception raised by put(block=0)/put_nowait() | |
| 15 Full = 'Queue.Full' | |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 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
|
18 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
|
19 """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
|
20 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
21 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
|
22 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
23 import thread |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
24 self._init(maxsize) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 self.fsema = thread.allocate_lock() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
30 def qsize(self): |
| 10120 | 31 """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
|
32 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
|
33 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
|
34 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
|
35 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
37 def empty(self): |
| 10120 | 38 """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
|
39 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
|
40 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
|
41 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
|
42 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
44 def full(self): |
| 10120 | 45 """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
|
46 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
|
47 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
|
48 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
|
49 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
| 10120 | 51 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
|
52 """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
|
53 |
| 10120 | 54 If optional arg 'block' is 1 (the default), block if |
| 55 necessary until a free slot is available. Otherwise (block | |
| 56 is 0), put an item on the queue if a free slot is immediately | |
| 57 available, else raise the Full exception. | |
| 58 """ | |
| 59 if block: | |
| 60 self.fsema.acquire() | |
| 61 elif not self.fsema.acquire(0): | |
| 62 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
|
63 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
|
64 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
|
65 self._put(item) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 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
|
70 self.mutex.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
| 10120 | 72 def put_nowait(self, item): |
| 73 """Put an item into the queue without blocking. | |
| 74 | |
| 75 Only enqueue the item if a free slot is immediately available. | |
| 76 Otherwise raise the Full exception. | |
| 77 """ | |
| 78 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
|
79 |
| 10120 | 80 def get(self, block=1): |
| 81 """Remove and return an item from the queue. | |
| 82 | |
| 83 If optional arg 'block' is 1 (the default), block if | |
| 84 necessary until an item is available. Otherwise (block is 0), | |
| 85 return an item if one is immediately available, else raise the | |
| 86 Empty exception. | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
87 """ |
| 10120 | 88 if block: |
| 89 self.esema.acquire() | |
| 90 elif not self.esema.acquire(0): | |
| 91 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
|
92 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
|
93 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
|
94 item = self._get() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 return item |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
102 def get_nowait(self): |
| 10120 | 103 """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
|
104 |
| 10120 | 105 Only get an item if one is immediately available. Otherwise |
| 106 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
|
107 """ |
| 10120 | 108 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
|
109 |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
110 # 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
|
111 # (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
|
112 # 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
|
113 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
114 # 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
|
115 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
|
116 self.maxsize = maxsize |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
117 self.queue = [] |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
119 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
|
120 return len(self.queue) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
122 # 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
|
123 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
|
124 return not self.queue |
|
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 # 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
|
127 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
|
128 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
|
129 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
130 # 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
|
131 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
|
132 self.queue.append(item) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
134 # 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
|
135 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
|
136 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
|
137 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
|
138 return item |
