annotate Lib/Queue.py @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents a195962e389c
children eef321b6e41c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
8 class Full(Exception):
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
12 # exception raised by get(block=0)/get_nowait()
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
13 Empty = 'Queue.Empty'
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
14 # exception raised by put(block=0)/put_nowait()
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
54 If optional arg 'block' is 1 (the default), block if
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
55 necessary until a free slot is available. Otherwise (block
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
56 is 0), put an item on the queue if a free slot is immediately
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
57 available, else raise the Full exception.
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
58 """
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
59 if block:
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
60 self.fsema.acquire()
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
61 elif not self.fsema.acquire(0):
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
72 def put_nowait(self, item):
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
73 """Put an item into the queue without blocking.
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
74
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
75 Only enqueue the item if a free slot is immediately available.
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
76 Otherwise raise the Full exception.
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
77 """
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
80 def get(self, block=1):
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
81 """Remove and return an item from the queue.
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
82
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
83 If optional arg 'block' is 1 (the default), block if
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
84 necessary until an item is available. Otherwise (block is 0),
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
85 return an item if one is immediately available, else raise the
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
88 if block:
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
89 self.esema.acquire()
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
90 elif not self.esema.acquire(0):
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
105 Only get an item if one is immediately available. Otherwise
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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
5b15642e7bdf Tim Peters:
Guido van Rossum <guido@python.org>
parents: 8197
diff changeset
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