Types of Queues
Explore the concept and implementations of queues in Python, focusing on FIFO behavior. Understand how to use list, collections.deque, and queue modules for single and multithreaded applications. Learn to create uniform queue interfaces and choose the best implementation based on your needs.
We'll cover the following...
Overview
Let’s have a look at an application of the list structure to create a queue. A queue is a special kind of buffer, summarized as First In First Out (FIFO). The idea is to act as a temporary stash so one part of an application can write to the queue while another part consumes items from the queue.
A database might have a queue of data to be written to disk. When our application performs an update, the local cache version of the data is updated so all other applications can see the change. However, the write to the disk may be placed in a queue for a writer to deal with a few milliseconds later.
When we’re looking at files and directories, a queue can be a handy place to stash details of the directories so they can be processed later. We’ll often represent a directory as the path from the root of the filesystem to the file of interest. The algorithm works like this:
We can visualize this list-like structure as growing via an append() and shrinking via pop(0). It would look like this:
Implementation
The idea is for the queue to grow and shrink: each directory grows the queue, and each file shrinks the queue. Eventually, all the files and directories have been processed, and ...