Class Queue implements queue objects and has the methods described below. This class can be derived from in order to implement other queue organizations (e.g. stack) but the inheritable interface is not described here. See the source code for details. The public methods are:
) |
) |
True
if the queue is empty, False
otherwise.
Because of multithreading semantics, this is not reliable.
) |
True
if the queue is full, False
otherwise.
Because of multithreading semantics, this is not reliable.
item[, block[, timeout]]) |
New in version 2.3: the timeout parameter.
item) |
put(item, False)
.
[block[, timeout]]) |
New in version 2.3: the timeout parameter.
) |
get(False)
.
Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.
) |
If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).
Raises a ValueError if called more times than there were items placed in the queue. New in version 2.5.
) |
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks. New in version 2.5.
Example of how to wait for enqueued tasks to be completed:
def worker(): while True: item = q.get() do_work(item) q.task_done() q = Queue() for i in range(num_worker_threads): t = Thread(target=worker) t.setDaemon(True) t.start() for item in source(): q.put(item) q.join() # block until all tasks are done