
18.5.8. Queues¶
A queue, useful for coordinating producer and consumer coroutines.
If maxsize is less than or equal to zero, the queue size is infinite. If it is an integer greater than , then will block when the queue reaches maxsize, until an item is removed by .
Unlike the standard library , you can reliably know this Queue’s size with , since your single-threaded asyncio application won’t be interrupted between calling and doing an operation on the Queue.
This class is not thread safe.
- ()¶
Return if the queue is empty, otherwise.
- ()¶
Return if there are items in the queue.
Note
If the Queue was initialized with (the default), then is never .
- coroutine ()¶
Remove and return an item from the queue. If queue is empty, wait until an item is available.
This method is a coroutine.
- ()¶
Remove and return an item from the queue.
Return an item if one is immediately available, else raise .
- coroutine ()¶
Block until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, unblocks.
This method is a coroutine.
- coroutine (item)¶
Put an item into the queue. If the queue is full, wait until a free slot is available before adding item.
This method is a coroutine.
- (item)¶
Put an item into the queue without blocking.
If no free slot is immediately available, raise .
- ()¶
Number of items in the queue.
- ()¶
Indicate that a formerly enqueued task is complete.
Used by queue consumers. For each used to fetch a task, a subsequent call to tells the queue that the processing on the task is complete.
If a is currently blocking, it will resume when all items have been processed (meaning that a call was received for every item that had been into the queue).
Raises if called more times than there were items placed in the queue.
- ¶
Number of items allowed in the queue.
Python Queue.get() Examples
The following are 25 code examples for showing how to use Queue.get(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module Queue, or try the search function .
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
Example 8
Example 9
Example 10
Example 11
Example 12
Example 13
Example 14
Example 15
Example 16
Example 17
Example 18
Example 19
Example 20
Example 21
Example 22
Example 23
Example 24
Example 25
- Google cloud functions context
- Hicks gas pay online
- Light fade haircut
- Chase bank line avenue
- Solar post cap
17.7. — A synchronized queue class¶
Source code:Lib/queue.py
The module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the module.
The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the module) and the lowest valued entry is retrieved first.
Internally, the module uses locks to temporarily block competing threads; however, it is not designed to handle reentrancy within a thread.
The module defines the following classes and exceptions:
- class (maxsize=0)¶
Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
- class (maxsize=0)¶
Constructor for a LIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
- class (maxsize=0)¶
Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
The lowest valued entries are retrieved first (the lowest valued entry is the one returned by ). A typical pattern for entries is a tuple in the form: .
- exception ¶
Exception raised when non-blocking (or ) is called on a object which is empty.
- exception ¶
Exception raised when non-blocking (or ) is called on a object which is full.
17.7.1. Queue Objects¶
Queue objects (, , or ) provide the public methods described below.
- ()¶
Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.
- ()¶
Return if the queue is empty, otherwise. If empty() returns it doesn’t guarantee that a subsequent call to put() will not block. Similarly, if empty() returns it doesn’t guarantee that a subsequent call to get() will not block.
- ()¶
Return if the queue is full, otherwise. If full() returns it doesn’t guarantee that a subsequent call to get() will not block. Similarly, if full() returns it doesn’t guarantee that a subsequent call to put() will not block.
- (item, block=True, timeout=None)¶
Put item into the queue. If optional args block is true and timeout is (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the exception (timeout is ignored in that case).
- (item)¶
Equivalent to .
- (block=True, timeout=None)¶
Remove and return an item from the queue. If optional args block is true and timeout is (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 exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the exception (timeout is ignored in that case).
- ()¶
Equivalent to .
Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.
- ()¶
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each used to fetch a task, a subsequent call to tells the queue that the processing on the task is complete.
If a is currently blocking, it will resume when all items have been processed (meaning that a call was received for every item that had been into the queue).
Raises a if called more times than there were items placed in the queue.
- ()¶
Blocks until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, unblocks.
Example of how to wait for enqueued tasks to be completed:
What is Python Queue?
A queue is a container that holds data. The data that is entered first will be removed first, and hence a queue is also called “First in First Out” (FIFO). The queue has two ends front and rear. The items are entered from the rear and removed from the front side.
In this Python tutorial, you will learn:
How does Python Queue Work?
The queue can be easily compared with the real-world example the line of people waiting in a queue at the ticket counter, the person standing first will get the ticket first, followed by the next person and so on. The same logic goes for the queue data structure too.
Here is a diagrammatic representation of queue:
The Rear represents the point where the items are inserted inside the queue. In this example, 7 is value for that.
The Front represents the point where the items from the queue will be removed. If you remove an item from the queue, the first element you will get is 1, as shown in the figure.
Item 1 was the first one to be inserted in the queue, and while removing it is the first one to come out. Hence the queue is called FIRST IN FIRST OUT (FIFO)
In a queue, the items are removed in order and cannot be removed from in between. You just cannot remove the item 5 randomly from the queue, to do that you will have to remove all the items before 5. The items in queue will be removed in the order they are inserted.
Types of Queue in Python
There are mainly two types of queue in Python:
- First in First out Queue: For this, the element that goes first will be the first to come out.
To work with FIFO, you have to call Queue() class from queue module.
- Last in First out Queue: Over here, the element that is entered last will be the first to come out.
To work with LIFO, you have to call LifoQueue() class from the queue module.
Python queue Installation
It is very easy to work with queue in python. Here are the steps to follow to make use of queue in your code.
Step 1) You just have to import the queue module, as shown below:
import queueThe module is available by default with python, and you don’t need any additional installation to start working with the queue. There are 2 types of queue FIFO (first in first out) and LIFO (last in first out).
Step 2) To work with FIFO queue , call the Queue class using the queue module imported as shown below:
import queue q1 = queue.Queue()Step 3) To work with LIFO queue call the LifoQueue() class as shown below:
import queue q1 = queue.LifoQueue()Methods available inside Queue and LifoQueue class
Following are the important methods available inside Queue and LifoQueue class:
- put(item): This will put the item inside the queue.
- get(): This will return you an item from the queue.
- empty(): It will return true if the queue is empty and false if items are present.
- qsize(): returns the size of the queue.
- full(): returns true if the queue is full, otherwise false.
First In First Out Queue Example
In the case of first in first out, the element that goes first will be the first to come out.
Add and item in a queue
Let us work on an example to add an item in a queue. To start working with the queue, first import the module queue, as shown in the example below.
To add an item , you can make use of put() method as shown in the example:
import queue q1 = queue.Queue() q1.put(10) #this will additem 10 to the queue.By default, the size of the queue is infinite and you can add any number of items to it. In case you want to define the size of the queue the same can be done as follows
import queue q1 = queue.Queue(5) #The max size is 5. q1.put(1) q1.put(2) q1.put(3) q1.put(4) q1.put(5) print(q1.full()) # will return true.Output:
TrueNow the size of the queue is 5, and it will not take more than 5 items, and the method q1.full() will return true. Adding any more items will not execute the code any further.
Remove an item from the queue
To remove an item from the queue, you can use the method called get(). This method allows items from the queue when called.
The following example shows how to remove an item from the queue.
import queue q1 = queue.Queue() q1.put(10) item1 = q1.get() print('The item removed from the queue is ', item1)Output:
The item removed from the queue is 10Last In First Out queue Example
In the case of last in the first out queue, the element that is entered last will be the first to come out.
To work with LIFO, i.e., last in the first out queue, we need to import the queue module and make use of the LifoQueue() method.
Add and item in a queue
Here we will understand how to add an item to the LIFO queue.
import queue q1 = queue.LifoQueue() q1.put(10)You have to use the put() method on LifoQueue, as shown in the above example.
Remove an item from the queue
To remove an item from the LIFOqueue you can make use of get() method .
import queue q1 = queue.LifoQueue() q1.put(10) item1 = q1.get() print('The item removed from the LIFO queue is ', item1)Output:
The item removed from the LIFO queue is 10Add more than 1 item in a Queue
In the above examples, we have seen how to add a single item and remove the item for FIFO and LIFOqueue. Now we will see how to add more than one item and also remove it.
Add and item in a FIFOqueue
import queue q1 = queue.Queue() for i in range(20): q1.put(i) # this will additem from 0 to 20 to the queueRemove an item from the FIFOqueue
import queue q1 = queue.Queue() for i in range(20): q1.put(i) # this will additem from 0 to 20 to the queue while not q1.empty(): print("The value is ", q1.get()) # get() will remove the item from the queue.Output:
The value is 0 The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9 The value is 10 The value is 11 The value is 12 The value is 13 The value is 14 The value is 15 The value is 16 The value is 17 The value is 18 The value is 19Add and item in a LIFOqueue
import queue q1 = queue.LifoQueue() for i in range(20): q1.put(i) # this will additem from 0 to 20 to the queueRemove an item from the LIFOqueue
import queue q1 = queue.LifoQueue() for i in range(20): q1.put(i) # this will additem from 0 to 20 to the queue while not q1.empty(): print("The value is ", q1.get()) # get() will remove the item from the queue.Output:
The value is 19 The value is 18 The value is 17 The value is 16 The value is 15 The value is 14 The value is 13 The value is 12 The value is 11 The value is 10 The value is 9 The value is 8 The value is 7 The value is 6 The value is 5 The value is 4 The value is 3 The value is 2 The value is 1 The value is 0Sorting Queue
Following example shows the queue sorting.The algorithm used for sorting is bubble sort.
import queue q1 = queue.Queue() #Addingitems to the queue q1.put(11) q1.put(5) q1.put(4) q1.put(21) q1.put(3) q1.put(10) #using bubble sort on the queue n = q1.qsize() for i in range(n): x = q1.get() # the element is removed for j in range(n-1): y = q1.get() # the element is removed if x > y : q1.put(y) #the smaller one is put at the start of the queue else: q1.put(x) # the smaller one is put at the start of the queue x = y # the greater one is replaced with x and compared again with nextelement q1.put(x) while (q1.empty() == False): print(q1.queue[0], end = " ") q1.get()Output:
3 4 5 10 11 21Reversing Queue
To reverse the queue, you can make use of another queue and recursion.
The following example shows how to get the queue reversed.
Example:
import queue q1 = queue.Queue() q1.put(11) q1.put(5) q1.put(4) q1.put(21) q1.put(3) q1.put(10) def reverseQueue (q1src, q2dest) : buffer = q1src.get() if (q1src.empty() == False) : reverseQueue(q1src, q2dest) #using recursion q2dest.put(buffer) return q2dest q2dest = queue.Queue() qReversed = reverseQueue(q1,q2dest) while (qReversed.empty() == False): print(qReversed.queue[0], end = " ") qReversed.get()Output:
10 3 21 4 5 11Summary:
- A queue is a container that holds data. There are two types of Queue, FIFO, and LIFO.
- For a FIFO (First in First out Queue), the element that goes first will be the first to come out.
- For a LIFO (Last in First out Queue), the element that is entered last will be the first to come out.
- An item in a queue is added using the put(item) method.
- To remove an item, get() method is used.
Get python queue
The Put() Method Of Queue Class In Python
# Example Python program that uses put() method
# to add elements to a queue.Queue instance
import queue
import threading
import os
import sys
import random
import time
try:
# Function for writer thread
def writer(sq):
while(True):
data = random.randint(0, 9999);
time.sleep(1);
sq.put(data);
print("writer:added %d"%data);
# Function for reader thread
def reader(sq):
while(True):
data = sq.get();
time.sleep(1);
print("reader:removed %d"%data);
# Create a synchronized queue instance
sharedQueue = queue.Queue();
# Create reader and writer threads
threads = [None, None];
threads[0] = threading.Thread(target=reader, args=(sharedQueue,));
threads[1] = threading.Thread(target=writer, args=(sharedQueue,));
# Start the reader and writer threads
for thread in threads:
thread.start();
# Wait for the reader and writer threads to exit
for thread in threads:
thread.join();
except KeyboardInterrupt:
print('Keyboard interrupt received from user');
try:
sys.exit(0);
except:
os._exit(0);
Queue in Python
Like stack, queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Operations associated with queue are:
- Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)
- Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition – Time Complexity : O(1)
- Front: Get the front item from queue – Time Complexity : O(1)
- Rear: Get the last item from queue – Time Complexity : O(1)
Implementation
There are various ways to implement a queue in Python. This article covers the implementation of queue using data structures and modules from Python library.
Queue in Python can be implemented by the following ways:
- list
- collections.deque
- queue.Queue
Implementation using list
List is a Python’s built-in data structure that can be used as a queue. Instead of enqueue() and dequeue(), append() and pop() function is used. However, lists are quite slow for this purpose because inserting or deleting an element at the beginning requires shifting all of the other elements by one, requiring O(n) time.
Python3
|
Output:
Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list
Implementation using collections.deque
Queue in Python can be implemented using deque class from the collections module. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity. Instead of enqueue and deque, append() and popleft() functions are used.
Python3
|
Output:
Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque
Implementation using queue.Queue
Queue is built-in module of Python which is used to implement a queue. queue.Queue(maxsize) initializes a variable to a maximum size of maxsize. A maxsize of zero ‘0’ means a infinite queue. This Queue follows FIFO rule.
There are various functions available in this module:
- maxsize – Number of items allowed in the queue.
- empty() – Return True if the queue is empty, False otherwise.
- full() – Return True if there are maxsize items in the queue. If the queue was initialized with maxsize=0 (the default), then full() never returns True.
- get() – Remove and return an item from the queue. If queue is empty, wait until an item is available.
- get_nowait() – Return an item if one is immediately available, else raise QueueEmpty.
- put(item) – Put an item into the queue. If the queue is full, wait until a free slot is available before adding the item.
- put_nowait(item) – Put an item into the queue without blocking. If no free slot is immediately available, raise QueueFull.
- qsize() – Return the number of items in the queue.
Python3
|
Output:
You will also like:
- Buddhist mandala coloring pages
- Athena pro line
- Steelseries arctis 9 amazon
- Crown potteries co
- Jeep hardtop removal
- Controller resume examples
- Hocus pocus drawing images
- Fuji instax camera
- Ogun fire force
— A synchronized queue class¶
Source code:Lib/queue.py
The module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The class in this module implements all the required locking semantics.
The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the module) and the lowest valued entry is retrieved first.
Internally, those three types of queues use locks to temporarily block competing threads; however, they are not designed to handle reentrancy within a thread.
In addition, the module implements a “simple” FIFO queue type, , whose specific implementation provides additional guarantees in exchange for the smaller functionality.
The module defines the following classes and exceptions:
- class (maxsize=0)¶
Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
- class (maxsize=0)¶
Constructor for a LIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
- class (maxsize=0)¶
Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
The lowest valued entries are retrieved first (the lowest valued entry is the one returned by ). A typical pattern for entries is a tuple in the form: .
If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number:
fromdataclassesimportdataclass,[email protected](order=True)classPrioritizedItem:priority:intitem:Any=field(compare=False)
- class ¶
Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking.
- exception ¶
Exception raised when non-blocking (or ) is called on a object which is empty.
- exception ¶
Exception raised when non-blocking (or ) is called on a object which is full.
Queue Objects¶
Queue objects (, , or ) provide the public methods described below.
- ()¶
Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.
- ()¶
Return if the queue is empty, otherwise. If empty() returns it doesn’t guarantee that a subsequent call to put() will not block. Similarly, if empty() returns it doesn’t guarantee that a subsequent call to get() will not block.
- ()¶
Return if the queue is full, otherwise. If full() returns it doesn’t guarantee that a subsequent call to get() will not block. Similarly, if full() returns it doesn’t guarantee that a subsequent call to put() will not block.
- (item, block=True, timeout=None)¶
Put item into the queue. If optional args block is true and timeout is (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the exception (timeout is ignored in that case).
- (item)¶
Equivalent to .
- (block=True, timeout=None)¶
Remove and return an item from the queue. If optional args block is true and timeout is (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 exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the exception (timeout is ignored in that case).
Prior to 3.0 on POSIX systems, and for all versions on Windows, if block is true and timeout is , this operation goes into an uninterruptible wait on an underlying lock. This means that no exceptions can occur, and in particular a SIGINT will not trigger a .
- ()¶
Equivalent to .
Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.
- ()¶
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each used to fetch a task, a subsequent call to tells the queue that the processing on the task is complete.
If a is currently blocking, it will resume when all items have been processed (meaning that a call was received for every item that had been into the queue).
Raises a if called more times than there were items placed in the queue.
- ()¶
Blocks until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, unblocks.
Example of how to wait for enqueued tasks to be completed:
SimpleQueue Objects¶
objects provide the public methods described below.
- ()¶
Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block.
- ()¶
Return if the queue is empty, otherwise. If empty() returns it doesn’t guarantee that a subsequent call to get() will not block.
- (item, block=True, timeout=None)¶
Put item into the queue. The method never blocks and always succeeds (except for potential low-level errors such as failure to allocate memory). The optional args block and timeout are ignored and only provided for compatibility with .
CPython implementation detail: This method has a C implementation which is reentrant. That is, a or call can be interrupted by another call in the same thread without deadlocking or corrupting internal state inside the queue. This makes it appropriate for use in destructors such as methods or callbacks.
- (item)¶
Equivalent to , provided for compatibility with .
- (block=True, timeout=None)¶
Remove and return an item from the queue. If optional args block is true and timeout is (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 exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the exception (timeout is ignored in that case).
- ()¶
Equivalent to .
See also
- Class
A queue class for use in a multi-processing (rather than multi-threading) context.
is an alternative implementation of unbounded queues with fast atomic and operations that do not require locking and also support indexing.