Mercurial > cpython-lru_cache-weakref
annotate Lib/Queue.py @ 33091:48100a01ac7c 2.3
Ignore the locale's encoding if it is an empty string.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 12 Aug 2004 13:14:20 +0000 |
| parents | 58132c813d7b |
| children | 63fa60f2d61c |
| 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 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
3 from time import time as _time, sleep as _sleep |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
4 |
| 29711 | 5 __all__ = ['Empty', 'Full', 'Queue'] |
| 6 | |
|
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
|
7 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
|
8 "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
|
9 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
|
10 |
|
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 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
|
12 "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
|
13 pass |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 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
|
16 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
|
17 """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
|
18 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
19 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
|
20 """ |
|
26892
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
21 try: |
|
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
22 import thread |
|
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
23 except ImportError: |
|
95510f0a0624
Use the dummy_thread module in Queue.py and tempfile.py.
Guido van Rossum <guido@python.org>
parents:
25796
diff
changeset
|
24 import dummy_thread as thread |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
25 self._init(maxsize) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 self.fsema = thread.allocate_lock() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
31 def qsize(self): |
| 10120 | 32 """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
|
33 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
|
34 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
|
35 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
|
36 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
38 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
|
39 """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
|
40 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
|
41 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
|
42 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
|
43 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
45 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
|
46 """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
|
47 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
|
48 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
|
49 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
|
50 return n |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
52 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
|
53 """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
|
54 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 is ignored in that case). |
| 10120 | 62 """ |
| 63 if block: | |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
64 if timeout is None: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
65 # blocking, w/o timeout, i.e. forever |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
66 self.fsema.acquire() |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
67 elif timeout >= 0: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
68 # waiting max. 'timeout' seconds. |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
69 # this code snipped is from threading.py: _Event.wait(): |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
70 # Balancing act: We can't afford a pure busy loop, so we |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
71 # have to sleep; but if we sleep the whole timeout time, |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
72 # we'll be unresponsive. The scheme here sleeps very |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
73 # little at first, longer as time goes on, but never longer |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
74 # than 20 times per second (or the timeout time remaining). |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
75 delay = 0.0005 # 500 us -> initial delay of 1 ms |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
76 endtime = _time() + timeout |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
77 while True: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
78 if self.fsema.acquire(0): |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
79 break |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
80 remaining = endtime - _time() |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
81 if remaining <= 0: #time is over and no slot was free |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
82 raise Full |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
83 delay = min(delay * 2, remaining, .05) |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
84 _sleep(delay) #reduce CPU usage by using a sleep |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
85 else: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
86 raise ValueError("'timeout' must be a positive number") |
| 10120 | 87 elif not self.fsema.acquire(0): |
| 88 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
|
89 self.mutex.acquire() |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
90 release_fsema = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
91 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
92 was_empty = self._empty() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
93 self._put(item) |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
94 # If we fail before here, the empty state has |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
95 # not changed, so we can skip the release of esema |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
96 if was_empty: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
97 self.esema.release() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
98 # If we fail before here, the queue can not be full, so |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
99 # release_full_sema remains True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
100 release_fsema = not self._full() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
101 finally: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
102 # Catching system level exceptions here (RecursionDepth, |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
103 # OutOfMemory, etc) - so do as little as possible in terms |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
104 # of Python calls. |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
105 if release_fsema: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
106 self.fsema.release() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
107 self.mutex.release() |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
| 10120 | 109 def put_nowait(self, item): |
| 110 """Put an item into the queue without blocking. | |
| 111 | |
| 112 Only enqueue the item if a free slot is immediately available. | |
| 113 Otherwise raise the Full exception. | |
| 114 """ | |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
115 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
|
116 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
117 def get(self, block=True, timeout=None): |
| 10120 | 118 """Remove and return an item from the queue. |
| 119 | |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 """ |
| 10120 | 128 if block: |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
129 if timeout is None: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
130 # blocking, w/o timeout, i.e. forever |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
131 self.esema.acquire() |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
132 elif timeout >= 0: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
133 # waiting max. 'timeout' seconds. |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
134 # this code snipped is from threading.py: _Event.wait(): |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
135 # Balancing act: We can't afford a pure busy loop, so we |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
136 # have to sleep; but if we sleep the whole timeout time, |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
137 # we'll be unresponsive. The scheme here sleeps very |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
138 # little at first, longer as time goes on, but never longer |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
139 # than 20 times per second (or the timeout time remaining). |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
140 delay = 0.0005 # 500 us -> initial delay of 1 ms |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
141 endtime = _time() + timeout |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
142 while 1: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
143 if self.esema.acquire(0): |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
144 break |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
145 remaining = endtime - _time() |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
146 if remaining <= 0: #time is over and no element arrived |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
147 raise Empty |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
148 delay = min(delay * 2, remaining, .05) |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
149 _sleep(delay) #reduce CPU usage by using a sleep |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
150 else: |
|
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
151 raise ValueError("'timeout' must be a positive number") |
| 10120 | 152 elif not self.esema.acquire(0): |
| 153 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
|
154 self.mutex.acquire() |
|
23160
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
155 release_esema = True |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
156 try: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
157 was_full = self._full() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
158 item = self._get() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
159 # If we fail before here, the full state has |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
160 # not changed, so we can skip the release of fsema |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
161 if was_full: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
162 self.fsema.release() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
163 # Failure means empty state also unchanged - release_esema |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
164 # remains True. |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
165 release_esema = not self._empty() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
166 finally: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
167 if release_esema: |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
168 self.esema.release() |
|
c548e726b3bc
Fix bug 544473 - "Queue module can deadlock".
Mark Hammond <mhammond@skippinet.com.au>
parents:
16657
diff
changeset
|
169 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
|
170 return item |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
172 def get_nowait(self): |
| 10120 | 173 """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
|
174 |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
175 Only get an item if one is immediately available. Otherwise |
| 10120 | 176 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
|
177 """ |
|
25796
3eafbde56b21
Patch #572628: Optional timeouts for put and get.
Martin v. Löwis <martin@v.loewis.de>
parents:
24058
diff
changeset
|
178 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
|
179 |
|
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
180 # 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
|
181 # (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
|
182 # 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
|
183 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
184 # 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
|
185 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
|
186 self.maxsize = maxsize |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6612
diff
changeset
|
187 self.queue = [] |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
189 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
|
190 return len(self.queue) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
192 # 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
|
193 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
|
194 return not self.queue |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
196 # 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
|
197 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
|
198 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
|
199 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
200 # 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
|
201 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
|
202 self.queue.append(item) |
|
995
f025eef93130
New module implementing a multi-everything queue.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 |
|
6612
4b2d8dc92db4
(Queue.Empty): When class based exceptions are in force, derive this
Barry Warsaw <barry@python.org>
parents:
1463
diff
changeset
|
204 # 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
|
205 def _get(self): |
|
24058
37ffce2d9c3c
Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents:
23160
diff
changeset
|
206 return self.queue.pop(0) |
