Create a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite.
| 38 | |
| 39 | |
| 40 | class Queue: |
| 41 | '''Create a queue object with a given maximum size. |
| 42 | |
| 43 | If maxsize is <= 0, the queue size is infinite. |
| 44 | ''' |
| 45 | |
| 46 | def __init__(self, maxsize=0): |
| 47 | self.maxsize = maxsize |
| 48 | self._init(maxsize) |
| 49 | |
| 50 | # mutex must be held whenever the queue is mutating. All methods |
| 51 | # that acquire mutex must release it before returning. mutex |
| 52 | # is shared between the three conditions, so acquiring and |
| 53 | # releasing the conditions also acquires and releases mutex. |
| 54 | self.mutex = threading.Lock() |
| 55 | |
| 56 | # Notify not_empty whenever an item is added to the queue; a |
| 57 | # thread waiting to get is notified then. |
| 58 | self.not_empty = threading.Condition(self.mutex) |
| 59 | |
| 60 | # Notify not_full whenever an item is removed from the queue; |
| 61 | # a thread waiting to put is notified then. |
| 62 | self.not_full = threading.Condition(self.mutex) |
| 63 | |
| 64 | # Notify all_tasks_done whenever the number of unfinished tasks |
| 65 | # drops to zero; thread waiting to join() is notified to resume |
| 66 | self.all_tasks_done = threading.Condition(self.mutex) |
| 67 | self.unfinished_tasks = 0 |
| 68 | |
| 69 | # Queue shutdown state |
| 70 | self.is_shutdown = False |
| 71 | |
| 72 | def task_done(self): |
| 73 | '''Indicate that a formerly enqueued task is complete. |
| 74 | |
| 75 | Used by Queue consumer threads. For each get() used to fetch a task, |
| 76 | a subsequent call to task_done() tells the queue that the processing |
| 77 | on the task is complete. |
| 78 | |
| 79 | If a join() is currently blocking, it will resume when all items |
| 80 | have been processed (meaning that a task_done() call was received |
| 81 | for every item that had been put() into the queue). |
| 82 | |
| 83 | Raises a ValueError if called more times than there were items |
| 84 | placed in the queue. |
| 85 | ''' |
| 86 | with self.all_tasks_done: |
| 87 | unfinished = self.unfinished_tasks - 1 |
| 88 | if unfinished <= 0: |
| 89 | if unfinished < 0: |
| 90 | raise ValueError('task_done() called too many times') |
| 91 | self.all_tasks_done.notify_all() |
| 92 | self.unfinished_tasks = unfinished |
| 93 | |
| 94 | def join(self): |
| 95 | '''Blocks until all items in the Queue have been gotten and processed. |
| 96 | |
| 97 | The count of unfinished tasks goes up whenever an item is added to the |
no outgoing calls
no test coverage detected
searching dependent graphs…