The queue
module in the standard library provides an efficient, thread-safe implementation of a first-in-first-out (FIFO) data structure.
A queue is a data structure where the first item stored is the first item retrieved, also known as "first in, first out". The Queue
class provided in the queue
module is a synchronized FIFO queue that is ideal for use when multiple threads or processes need to share data.
Apart from the Queue
class, the module provides other classes that are derived from the Queue
class. The classes in the module ar as summarized below:
class | description |
---|---|
Queue |
An implementation of the FIFO queue data structure. |
LifoQue |
A subclass of Queue . It creates Last-In-First-Out queues, where the last added item is the first retrieved item. |
PriorityQueue |
A subclass of Queue . This type of queue stores items according to their priority, so that the highest priority item is retrieved first. |
SimpleQueue |
A simplified version of Queue . It provides basic methods for creating and managing queues. |
The Queue Class
The Queue
class is a thread-safe FIFO queue data structure , meaning that multiple threads can simultaneously access and manipulate the queue without fear of data corruption. To instantiate Queue objects, we use the following syntax.
Queue(maxsize = None)
maxsize |
An optional integer specifying the maximum size of the queue. If maxsize is None or the value se is <= 0 , the queue size is unbounded and it can grow to an arbitrary size. If an item is added to a full queue, a QueueFull exception is raised. |
#import the class
from queue import Queue
q = Queue(maxsize = 5)
#add items to the queue
q.put(10)
q.put(20)
q.put(30)
q.put(40)
q.put(50)
#get elements from the queue
print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
The put()
method is used to add an element at the back of the queue while the get()
method removes and returns the earliest item in the queue.
Check whether the queue is empty/full
The empty()
method returns a boolean value indicating whether the queue is empty or not. If empty it returns True
, otherwise False
.
Check whether the queue is empty
from queue import Queue
q = Queue()
print(q.empty())
q.put(10)
q.put(20)
print(q.empty())
The full()
method checks whether the maximum size is reached.
from queue import Queue
q = Queue(3)
print(q.full())
q.put(10)
q.put(20)
q.put(30)
print(q.full())
The Queue methods
The following table summarizes the Queue
methods.
method | description |
---|---|
get(block = True, timeout=None) |
Removes the front item from the Queue. If block is True and timeout is None (the default), block if necessary until an item is available. |
get_nowait() |
Equivalent to get(block = False) |
put(item, block=True, timeout=None) |
Puts an item at the back of the queue. If the optional argument block is True and timeout is None (the default), block if necessary until a free slot is available |
put_nowait(item) |
Put an item into the queue without blocking. equivalent to put(item block = None) |
empty() |
Returns True if the queue is empty, else False . |
full() |
Returns True if the maxsize is reached, else False . |
qsize() |
Returns an approximate number of elements that are currently in the queue. |
task_done() |
Indicates that a formerly enqueued task is complete. |
join() |
Block until all items in the queue have been retrieved and processed. |
Note that the Queue
class is made to be used by multiple threads at once, that is why we have the block
and timeout
parameters to control how long each thread must wait before returning with no items in the queue.
The LifoQueue class
The LifoQueue
class is a subclass of the Queue
class. It implements a type of queue where the last item in the queue is the first to be removed, this is also known as a stack
. The class
maintains the same basic functionality as the Queue class but adds additional functionality to support the LIFO queue structure.
from queue import LifoQueue
stack = LifoQueue()
stack.put(10)
stack.put(20)
stack.put(30)
stack.put(40)
stack.put(50)
#get items
print(stack.get())
print(stack.get())
print(stack.get())
print(stack.get())
print(stack.get())
The PriorityQueue class
The PriorityQueue
class implements a priority queue data structure. This type of data structure is used to store items in a queue based on their priority level.
The elements of PriorityQueue
objects are typically tuples in the form (priority, data)
.
from queue import PriorityQueue
q = PriorityQueue()
q.put((3, 'C++'))
q.put((5, 'Java'))
q.put((1, 'Python'))
q.put((4, 'PHP'))
q.put((2, 'Ruby'))
#get elements
print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())