-
One
Source Code: lib/queue.py
The queue module implements Multi-producer, Multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends on the availability of the thread support in Python; See the threading module.
The module implements three types of the queue, which differ only in the order in which the entries is retrieved. In a FIFO queue, the first tasks added is the first retrieved. In a LIFO queue, the most recently added entry are the first retrieved (operating like a stack). With a priority queue, the entries was kept sorted (using the HEAPQ module) and the lowest valued entry is Retrie Ved first.
The queue module defines the following classes and exceptions:
-
-
class queue. Queue (
maxsize=0 )
-
Constructor for a FIFO queue. maxsize is a integer that sets the upperbound limit on the number of items the can being placed in the queue. Insertion would block once this size have been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
Queue objects (queue, lifoqueue, or priorityqueue) provide the public methods described below.
- queue. qsize ( )
-
Return The approximate size of the queue. Note, qsize () > 0 doesn ' t guarantee that a subsequent get () would not block, nor would qsize () < maxsize guarantee Tha T put () would not block.
- queue. empty ( )
-
Return true if th E queue is empty, false otherwise. If empty () returns true it doesn ' t guarantee that a Subsequent call to put () would not block. Similarly, if empty () returns false it doesn ' t guarantee That a subsequent call to get () would not block.
- queue. full ( )
-
Return true if th E queue is full, false otherwise. If Full () returns true it doesn ' t guarantee Subsequent call to get () would not block. Similarly, if full () returns false it doesn ' t guarantee That's a subsequent call to put () would not block.
- queue. put (
item ,
block=true ,
timeout=none )
-
Put item into the queue. If optional args block is true and timeout are None (the default), block if necessary until a free slot I S available. If Timeout is a positive number, it blocks at most timeout seconds and raises the full exception if no free slot is available within that time. Otherwise ( block is false), put an item on the queue if a free slot is immediately available, else raise the full Exception ( timeout is ignored in That case).
- queue. put_nowait (
item )
-
Equivalent to put (item, false) .
- queue. get (
block=true ,
timeout=none )
-
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until a item is AV Ailable. If Timeout is a positive number, it blocks at most timeout seconds and raises the empty exception if no item is available within that time. Otherwise ( block is false), return a item if one is immediately available, else raise the empty Exception ( timeout is ignored in).
- Queue. get_nowait ( )
-
Equivalent to get (False).
The methods is offered to the support tracking whether enqueued tasks has been fully processed by daemon consumer threads.
-
- Queue. Task_done ( )
-
-
Indicate a formerly enqueued task is complete. Used by queue consumer threads. For each get () used to fetch a task, a Subsequent call to task_done () tells the The queue, the processing on the task was complete.
If a join () is currently blocking, it Would resume when all items has been processed (meaning that a task_done () call is 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.
- Queue. Join ( )
-
Blocks until all items in the queue has been gotten and processed.
The count of unfinished tasks goes up whenever an item are added to the queue. The count goes down whenever a consumer thread calls Task_done () to indicate the item is retrieved and all Work on it are complete. When the count of unfinished tasks drops to zero, join () unblocks.
Example of how-to-wait for enqueued tasks to be completed:
DefWorker():WhileTrue:Item=Q.Get()Do_work(Item)Q.Task_done()Q=Queue()ForIInchRange(num_worker_threads): t = Thread(target=worker) T. Daemon = True t. Start()for item in source(): q. Put(item)Q. Join() # block until all Tasks is done
-
-
-
-
Second:
-
-
class multiprocessing. Queue ( [
maxsize])
-
Returns a process Shared queue implemented using a pipe and a few locks/semaphores. When a process first puts an item on the queue a feeder thread was started which transfers objects from a buffer into the P Ipe.
The usual queue. Empty and queue. full exceptions from the standard library ' s Queue module is raised to signal timeouts.
Queue implements all the methods of the queue. Queue except for Task_done ( ) and join ().
- qsize ( )
-
Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable.
Note that this could raise notimplementederror on the Unix platforms like Mac OS X where sem_getvalue () is Not implemented.
- Empty ( )
-
Return True If the queue is empty, False otherwise. Because of Multithreading/multiprocessing semantics, this is not reliable.
- Full ( )
-
Return True If the queue is full, False otherwise. Because of Multithreading/multiprocessing semantics, this is not reliable.
-
- put (
obj [,
block [,
timeout ] )
-
-
Put obj into the queue. If the optional argument block is true (the Default) and timeout is none (the default), block If necessary until a free slots is available. If Timeout is a positive number, it blocks at most timeout seconds and raises the queue. Full exception if no free slot is available within that time. Otherwise ( block is false ), put a item on the Queue if a free slots is immediately available, else raise the queue. Full Exception ( timeout was ignored in).
- put_nowait (
obj )
-
Equivalent to put (obj, False).
-
- Get ( [
block[,
timeout]])
-
-
Remove and return an item from the queue. If Optional args block is true (the default) and Timeout is none (the default), block if necessary Until an item is available. If Timeout is a positive number, it blocks at most timeout seconds and raises the queue. Empty Exception if no item is available within that time. Otherwise (block is false ), return a item if one is Immediately available, else raise the queue. Empty Exception ( timeout is ignored in).
- get_nowait ( )
-
Equivalent to get (False).
multiprocessing. Queue has a few additional methods not found in queue. Queue. These methods is usually unnecessary for most code:
- Close ( )
-
Indicate that's no more data would be put in this queue by the current process. The background thread would quit once it had flushed all buffered data to the pipe. This was called automatically when the queue was garbage collected.
- join_thread ( )
-
Join the background thread. This can is used after close () have been called. It blocks until the background thread exits, ensuring that all data in the buffer have been flushed to the pipe.
By default if a process was not the creator of the queue then on exit it would attempt to join the queue ' s background thr ead. The process can call cancel_join_thread () to make join_thread () does nothing.
- Cancel_join_thread ( )
-
Prevent Join_thread () from blocking. In particular, this prevents the background thread from being joined automatically when the process Exits–see join_t Hread ().
A better name for this method might is Allow_exit_without_flush (). It is likely to cause enqueued data to lost, and you almost certainly would not need to use it. It is really only there if you need the current process to exit immediately without waiting to flush enqueued data to the Underlying pipe, and you don ' ts about lost data.
Python queue in two places