annotate lib/threads/Py25Queue.py @ 1849:301bc61643a9 math_module

merged changes from trunk
author lorenz
date Mon, 16 Nov 2009 18:31:40 +0000
parents 4bc6d1bf7ceb
children f7ba151c33c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1167
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
1 """A multi-producer, multi-consumer queue."""
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
2
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
3 from time import time as _time
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
4
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
5 from collections import deque
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
6
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
7 __all__ = ['Empty', 'Full', 'Queue']
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
8
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
9 class Empty(Exception):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
10 "Exception raised by Queue.get(block=0)/get_nowait()."
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
11 pass
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
12
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
13 class Full(Exception):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
14 "Exception raised by Queue.put(block=0)/put_nowait()."
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
15 pass
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
16
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
17 class Queue:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
18 """Create a queue object with a given maximum size.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
19
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
20 If maxsize is <= 0, the queue size is infinite.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
21 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
22 def __init__(self, maxsize=0):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
23 try:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
24 import threading
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
25 except ImportError:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
26 import dummy_threading as threading
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
27 self._init(maxsize)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
28 # mutex must be held whenever the queue is mutating. All methods
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
29 # that acquire mutex must release it before returning. mutex
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
30 # is shared between the three conditions, so acquiring and
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
31 # releasing the conditions also acquires and releases mutex.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
32 self.mutex = threading.Lock()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
33 # Notify not_empty whenever an item is added to the queue; a
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
34 # thread waiting to get is notified then.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
35 self.not_empty = threading.Condition(self.mutex)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
36 # Notify not_full whenever an item is removed from the queue;
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
37 # a thread waiting to put is notified then.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
38 self.not_full = threading.Condition(self.mutex)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
39 # Notify all_tasks_done whenever the number of unfinished tasks
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
40 # drops to zero; thread waiting to join() is notified to resume
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
41 self.all_tasks_done = threading.Condition(self.mutex)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
42 self.unfinished_tasks = 0
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
43
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
44 def task_done(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
45 """Indicate that a formerly enqueued task is complete.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
46
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
47 Used by Queue consumer threads. For each get() used to fetch a task,
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
48 a subsequent call to task_done() tells the queue that the processing
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
49 on the task is complete.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
50
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
51 If a join() is currently blocking, it will resume when all items
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
52 have been processed (meaning that a task_done() call was received
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
53 for every item that had been put() into the queue).
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
54
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
55 Raises a ValueError if called more times than there were items
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
56 placed in the queue.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
57 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
58 self.all_tasks_done.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
59 try:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
60 unfinished = self.unfinished_tasks - 1
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
61 if unfinished <= 0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
62 if unfinished < 0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
63 raise ValueError('task_done() called too many times')
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
64 self.all_tasks_done.notifyAll()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
65 self.unfinished_tasks = unfinished
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
66 finally:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
67 self.all_tasks_done.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
68
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
69 def join(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
70 """Blocks until all items in the Queue have been gotten and processed.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
71
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
72 The count of unfinished tasks goes up whenever an item is added to the
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
73 queue. The count goes down whenever a consumer thread calls task_done()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
74 to indicate the item was retrieved and all work on it is complete.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
75
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
76 When the count of unfinished tasks drops to zero, join() unblocks.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
77 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
78 self.all_tasks_done.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
79 try:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
80 while self.unfinished_tasks:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
81 self.all_tasks_done.wait()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
82 finally:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
83 self.all_tasks_done.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
84
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
85 def qsize(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
86 """Return the approximate size of the queue (not reliable!)."""
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
87 self.mutex.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
88 n = self._qsize()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
89 self.mutex.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
90 return n
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
91
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
92 def empty(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
93 """Return True if the queue is empty, False otherwise (not reliable!)."""
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
94 self.mutex.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
95 n = self._empty()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
96 self.mutex.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
97 return n
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
98
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
99 def full(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
100 """Return True if the queue is full, False otherwise (not reliable!)."""
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
101 self.mutex.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
102 n = self._full()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
103 self.mutex.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
104 return n
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
105
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
106 def put(self, item, block=True, timeout=None):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
107 """Put an item into the queue.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
108
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
109 If optional args 'block' is true and 'timeout' is None (the default),
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
110 block if necessary until a free slot is available. If 'timeout' is
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
111 a positive number, it blocks at most 'timeout' seconds and raises
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
112 the Full exception if no free slot was available within that time.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
113 Otherwise ('block' is false), put an item on the queue if a free slot
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
114 is immediately available, else raise the Full exception ('timeout'
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
115 is ignored in that case).
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
116 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
117 self.not_full.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
118 try:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
119 if not block:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
120 if self._full():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
121 raise Full
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
122 elif timeout is None:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
123 while self._full():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
124 self.not_full.wait()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
125 else:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
126 if timeout < 0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
127 raise ValueError("'timeout' must be a positive number")
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
128 endtime = _time() + timeout
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
129 while self._full():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
130 remaining = endtime - _time()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
131 if remaining <= 0.0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
132 raise Full
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
133 self.not_full.wait(remaining)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
134 self._put(item)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
135 self.unfinished_tasks += 1
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
136 self.not_empty.notify()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
137 finally:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
138 self.not_full.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
139
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
140 def put_nowait(self, item):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
141 """Put an item into the queue without blocking.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
142
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
143 Only enqueue the item if a free slot is immediately available.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
144 Otherwise raise the Full exception.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
145 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
146 return self.put(item, False)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
147
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
148 def get(self, block=True, timeout=None):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
149 """Remove and return an item from the queue.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
150
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
151 If optional args 'block' is true and 'timeout' is None (the default),
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
152 block if necessary until an item is available. If 'timeout' is
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
153 a positive number, it blocks at most 'timeout' seconds and raises
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
154 the Empty exception if no item was available within that time.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
155 Otherwise ('block' is false), return an item if one is immediately
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
156 available, else raise the Empty exception ('timeout' is ignored
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
157 in that case).
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
158 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
159 self.not_empty.acquire()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
160 try:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
161 if not block:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
162 if self._empty():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
163 raise Empty
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
164 elif timeout is None:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
165 while self._empty():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
166 self.not_empty.wait()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
167 else:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
168 if timeout < 0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
169 raise ValueError("'timeout' must be a positive number")
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
170 endtime = _time() + timeout
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
171 while self._empty():
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
172 remaining = endtime - _time()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
173 if remaining <= 0.0:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
174 raise Empty
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
175 self.not_empty.wait(remaining)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
176 item = self._get()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
177 self.not_full.notify()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
178 return item
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
179 finally:
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
180 self.not_empty.release()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
181
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
182 def get_nowait(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
183 """Remove and return an item from the queue without blocking.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
184
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
185 Only get an item if one is immediately available. Otherwise
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
186 raise the Empty exception.
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
187 """
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
188 return self.get(False)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
189
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
190 # Override these methods to implement other queue organizations
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
191 # (e.g. stack or priority queue).
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
192 # These will only be called with appropriate locks held
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
193
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
194 # Initialize the queue representation
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
195 def _init(self, maxsize):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
196 self.maxsize = maxsize
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
197 self.queue = deque()
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
198
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
199 def _qsize(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
200 return len(self.queue)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
201
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
202 # Check whether the queue is empty
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
203 def _empty(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
204 return not self.queue
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
205
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
206 # Check whether the queue is full
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
207 def _full(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
208 return self.maxsize > 0 and len(self.queue) == self.maxsize
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
209
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
210 # Put a new item in the queue
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
211 def _put(self, item):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
212 self.queue.append(item)
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
213
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
214 # Get an item from the queue
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
215 def _get(self):
4bc6d1bf7ceb Moved pygame.threads into it's own package due to Queue needing to import built-in time module, was conflicting with time.pyd in same dir.
akalias
parents:
diff changeset
216 return self.queue.popleft()