MCPcopy Index your code
hub / github.com/python/cpython / Queue

Class Queue

Lib/queue.py:40–277  ·  view source on GitHub ↗

Create a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite.

Source from the content-addressed store, hash-verified

38
39
40class Queue:
41 ''&#x27;Create a queue object with a given maximum size.
42
43 If maxsize is <= 0, the queue size is infinite.
44 ''&#x27;
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 ''&#x27;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 ''&#x27;
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 ''&#x27;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

Callers 1

deccheck.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…