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 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 need to handle; processing these work items could take a long time. Queues can also help us transmit long-running tasks while keeping a responsive user interface. We put an item on the queue for later processing and inform the user that 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 permits a finite amount of worker tasks. This can be used in cases in which we need to limit concurrency in a similar way to what we saw with semaphores in chapter 11.

12.1 Asynchronous queue basics

12.1.1 Queues in web applications

12.1.2 A web crawler queue

12.2 Priority queues

12.3 LIFO queues

Summary

sitemap