Queue

The Queue module provides a set of queue structures, which are efficient at adding and releasing values. A classic Queue object, also known as first-in, first-out (FIFO), provides a convenient way to manage things such as tasks. You don't need to worry about order of instances, as the first submitted task will be retrieved first, and so on.

Queues have a maxsize argument and will raise an exception if you try to add more elements than the maxsize value. They also have the .task_done() method, which you can run from your code, indicating that the task is done and allowing Queue to safely drop it, thus decreasing the number of tasks in the queue.

As queues are designed with multithreading in mind (multiple tasks running at the same time)queues also support waiting for the vacant space in the queue on put and get methods, maximum wait time can be defined by a timeout argument—infinite by default.

Let's have a look at the following example. First, we are creating a queue and adding two tasks into it. We can see the number of tasks by using the qsize method:

>>> from queue import Queue
>>> Q = Queue(maxsize=2)

>>> Q.put('wash dishes')
>>> Q.put('water flowers')

>>> Q.qsize()
2

As our Queue has a maximum size of 2, it is now full and if we attempt to add one more task it will wait (in our case, forever) for the existing tasks to be removed. We could, however, override this by adding another task with the put_nowait method if we wanted to. Now, let's pull tasks from the queue. The first task will be wash dishes, as it was the first to enter the queue:

>>> Q.get()
'wash dishes'

Once we have pulled the task, we can pretend that it is done and tell the queue. We can further check the number of tasks that need to be done via the following commands:

>>> Q.task_done()
>>> Q.qsize()
1

As the number is now smaller than the maximum, we can push new tasks without needing to wait for tasks to be removed from Queue:

>>> Q.put('check mail')
>>> Q.qsize()
2

A last-in, first-out (LIFO) queue, also known as a stack, works the same way, except that it releases the last submitted task first, so that the first submitted task will be retrieved last. A priority queue manages the order of tasks by the corresponding priority value.

Starting with Python 3.7, the Queue module also has SimpleQueue, which is a simpler FIFO data structure that cannot track tasks and, hence, does not have the .task_done() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset