SoFunction
Updated on 2025-03-01

Python queue module functions

Introduction to queue module

The queue module is a standard module built in Python. The module implements three types of queues. The difference is only the order of entry retrieval, which is represented by 3 classes, Queue, LifoQueue, and PriorityQueue.

The queue module is a standard built-in Python module and can be referenced directly through the import queue. Three synchronous, thread-safe queues are provided in the Queue module, represented by three classes Queue, LifoQueue and PriorityQueue. The only difference between them is that the order of elements is taken out is different. And LifoQueue and PriorityQueue are both subclasses of Queue.

1. Queue(FIFO queue)

The Queue class represents a basic FIFO (First In First Out) queue, that is, first in first out. The creation method is (maxsize=0), where maxsize is an integer, indicating the upper limit of the number of data that can be stored in the queue. Here is an example using Queue.

from queue import Queue
queue_object = Queue()
for i in range(4):
  queue_object.put(i)
while not queue_object.empty():
  print(queue_object.get())

In the above example, put 4 numbers in the Queue queue, and then take out its element values ​​in turn. Its running results are as follows:

0

1

2

3

2. LifoQueue (LIFO queue)

The LifoQueue class represents the last in First Out queue. Similar to the stack, both the later incoming elements are first out. The creation method is also very simple, just use (maxsize=0), where the meaning of maxsize is the same as that of Queue class. Here is an example using LifoQueue:

from queue import LifoQueue
lifo_queue = LifoQueue()
for i in range(4):
    lifo_queue.put(i)
while not lifo_queue.empty():
    print(lifo_queue.get())

The above example also places 4 numbers in LifoQueue, but the order of the elements is taken out is the opposite of the Queue, and the last element is taken out first. The operation results are as follows:

3
2
1
0

3. PriorityQueue (priority queue)

The PriorityQueue class represents a priority queue. Elements are taken in order of level, and those with the lowest level are taken out first. Elements in the priority queue are generally stored in the form of tuples (priority level, data). The creation method is also (maxsize=0). Here is an example using PriorityQueue:

from queue import PriorityQueue
class Job(object):
  def __init__(self, level, description):
      = level
      = description
     return
  def __lt__(self, other):
     return  < 
priority_queue = PriorityQueue()
priority_queue.put(Job(5, 'Medium-level job'))
priority_queue.put(Job(10, 'Low-level job'))
priority_queue.put(Job(1, 'Important Job'))
while not priority_queue.empty():
  next_job = priority_queue.get()
  print('Get started working:', next_job.description)

In the above example, the task job is stored in the PriorityQueue, and each job has a priority level. The lower the level value, the higher the priority. When calling the get method, the elements are removed from the queue in order of priority from high to low.

Its running results are as follows:

Start: Important Work
Start: Intermediate level work
Start: Low-level work

In addition, 2 exception classes are defined in the Queue module, as shown below:

·Empty: This exception can be thrown when fetching data from an empty queue.

·Full: This exception can be thrown when data is stored in a full queue.

Here is the article about what functions does the Python queue module have? That’s all for the article. For more related Python queue module content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!