In Python, the queue module provides thread-safe queue classes for managing tasks in multi-producer and multi-consumer environments. It allows threads to safely share work using FIFO, LIFO, or priority-based ordering.
In this chapter, we will discuss the queue module, its types, operations with examples, and built-in methods.
The queue module provides thread-safe queue implementations for safely managing data between multiple threads. It is commonly used for task scheduling, job management, and sharing work between threads in concurrent applications.
A queue is a linear data structure that stores items in First-In-First-Out (FIFO) order. The first item added is removed first, like a line of people waiting for their turn. New items are added at the rear, while items are removed from the front.

Queues are commonly used in ticket booking systems, printer task management, CPU scheduling, and customer service systems. They are useful when tasks need to be processed in the same order in which they are received.
The queue module provides four main queue classes: Queue, LifoQueue, PriorityQueue, and SimpleQueue:

A First-In-First-Out (FIFO) queue processes tasks in the same order in which they are added. It is created using queue.Queue(maxsize=0), where the maxsize parameter specifies the maximum number of items the queue can hold. If maxsize is less than or equal to zero, the queue has no size limit.
A Last-In-First-Out (LIFO) queue processes the most recently added item first, just like a stack. It is created using queue.LifoQueue(maxsize=0), where maxsize specifies the maximum number of items the queue can hold.
A PriorityQueue processes items based on their priority, rather than the order in which they are added. The item with the lowest priority value is retrieved first. It is created using queue.PriorityQueue(maxsize=0), where maxsize specifies the maximum number of items the queue can hold. Items are stored as tuples in the form of priority_number, data.
A SimpleQueue is an unbounded FIFO queue that processes items in the order they are added. It provides basic queue functionality without advanced features such as task tracking. It is created using queue.SimpleQueue().
We can perform some basic operations on a queue. These operations are discussed below:
Enqueue is the process of adding an item to the rear (end) of a queue. In a queue, new items are always added at the rear and removed from the front. It follows the First-In-First-Out (FIFO) principle.
Example
The following example demonstrates how to perform the enqueue operation in a queue.
Python
import queue
# Create a FIFO queue
q = queue.Queue()
# Enqueue items
q.put('a')
q.put('b')
q.put('c')
# Display in list by accessing the internal queue attribute
display(list(q.queue))
Output:
['a', 'b', 'c']
Explanation
In this example, we imported the queue module and created a queue variable named q. After that, we added elements one by one to the queue using the put() function. Finally, we displayed the result in a list.
Dequeue is the process of removing an item from the front of a queue. In a queue, the item that was added first is removed first. It follows the First-In-First-Out (FIFO) principle.
Example
The following example demonstrates how to perform the dequeue operation in a queue.
Python
import queue
# Create a FIFO queue
q = queue.Queue()
# Enqueue items
q.put('Hello!')
q.put('Tpoint')
q.put('Tech')
# Dequeue items
print(q.get())
print(q.get())
print(q.get())
Output:
Hello! Tpoint Tech
Explanation
In this example, we imported the queue module and created a queue variable named q. After that, we added some elements to the queue. Finally, we dequeued the elements one by one and displayed them using the get() function.
Peek is the process of viewing the item at the front of a queue without removing it. It allows us to check which item will be removed next while keeping the queue unchanged.
Example
The following example demonstrates how to perform the peek operation in a queue.
Python
import queue
# Create a FIFO queue
q = queue.Queue()
# Enqueue items
q.put('Hello!')
q.put('Tpoint')
q.put('Tech')
# Peeking at the first element without removing it
if not q.empty():
first_element = q.queue[0]
print(f"The first element in the queue (peeked): {first_element}")
else:
print("The queue is empty.")
Output:
The first element in the queue (peeked): Hello!
Explanation
In this example, we imported the queue module and created a queue variable named q. After that, we added some elements to the queue. Finally, we peeked at the first element and displayed it in the output.
isEmpty is an operation used to check whether a queue contains any elements or not. It returns True if the queue is empty, and False if the queue contains one or more elements.
Example
The following example demonstrates how to perform the isEmpty operation in a queue.
Python
import queue
# Create a FIFO queue
q = queue.Queue()
# Enqueue items
q.put('Hello!')
q.put('Tpoint')
q.put('Tech')
# Perform isEmpty operation
print(f"Is the queue empty?: {q.empty()}")
Output:
Is the queue empty?: False
Explanation
In this example, we imported the queue module and created a queue variable named q. After that, we added some elements to the queue. Finally, we checked whether the queue is empty using the empty() function. The output is False, which shows the queue is not empty.
Size is an operation used to determine the number of elements currently present in a queue. It helps us know how many items are waiting to be processed.
Example
The following example demonstrates how to determine the number of elements currently present in a queue.
Python
import queue
# Create a FIFO queue
q = queue.Queue()
# Enqueue items
q.put('Hello!')
q.put('Tpoint')
q.put('Tech')
# Find the number of elements present in a queue.
print(f"Number of elements in the queue: {q.qsize()}")
Output:
Number of elements in the queue: 3
Explanation
In this example, we imported the queue module and created a queue variable named q. After that, we added some elements to the queue. Finally, we find the size of the queue using the qsize() function and print it in the output.
The following table lists some important built-in methods of the Queue module for performing modifications.
| Methods | Description |
|---|---|
| put() | The put() method adds an item to the queue and performs the enqueue operation. By default, it waits if the queue is full until space becomes available. |
| get() | The get() method removes and returns an item from the queue. It performs the dequeue operation by retrieving the item that has been waiting in the queue for the longest time. |
| qsize() | The qsize() method returns the approximate number of items currently present in the queue. Because the queue can be accessed by multiple threads, the returned value should not be treated as a guaranteed snapshot. |
| empty() | The empty() method checks whether the queue is empty. It returns True if the queue contains no items; otherwise, it returns False. |
| full() | The full() method checks whether the queue is full. It returns True if the queue has reached its maximum size; otherwise, it returns False. |
| put_nowait() | The put_nowait() method adds an item to the queue without waiting. If the queue is full, it immediately raises a Full exception. |
| get_nowait() | The get_nowait() method removes and returns an item from the queue without waiting. If the queue is empty, it immediately raises an Empty exception. |
| task_done() | The task_done() method indicates that a previously retrieved item from the queue has been completely processed. It is used with join() to track when all queued tasks have been completed. |
| join() | The join() method blocks the program until all items in the queue have been processed and marked as completed using task_done(). |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India