12 Asynchronous queues
This chapter covers:
- Asynchronous queues
- Using queues for producer-consumer workflows
- Using queues with web applications
- Asynchronous priority queues
- Asynchronous LIFO queues
When designing applications to process events or other types of data, we often need a mechanism to store these events and distribute them to a set of workers. These workers can then do whatever we need to do based on these events concurrently, yielding time savings as opposed to processing events sequentially. Asyncio provides an asynchronous queue implementation that lets us do exactly this. We can add pieces of data into a queue and have several workers running concurrently, pulling data from the queue and processing it as it becomes available.
These are commonly referred to as producer-consumer workflows. Something produces data or events that we then need to handle and processing these work items could potentially take a long time. Queues can also help us send off long-running tasks while keeping a responsive user interface. We put an item on the queue for later processing, and let the user know we’ve started this work in the background. Asynchronous queues also have an added benefit of providing a mechanism to limit concurrency, as each queue generally has a finite amount of worker tasks. This can be used in cases where we need to limit concurrency similar to what we saw with semaphores in chapter 11.