chapter twelve

12 Asynchronous communication

 

In this chapter

  • You learn the difference between preemptive and cooperative multitasking
  • You learn how to implement asynchronous system using cooperative multitasking via coroutines and futures
  • You learn how you can combine event-based concurrency and concurrency primitives to implement an asynchronous system that can efficiently run both I/O and CPU tasks
  • You learn about asynchronous communication and understand when you should use an asynchronous model

We, people, are impatient by nature and always seem to want systems to have immediate feedback. But this is not always necessary. In many cases, we can simply postpone processing or move it elsewhere so that it happens asynchronously. When we do this, we reduce the latency constraints on systems that actually have to run in real time. Part of the goal of moving to asynchronous operations is to reduce the workload, but it's not always a simple step.

In this chapter we will learn how to implement asynchronous systems by inheriting the "event loop plus callback" mode described in the previous chapter and turns into its own implementation. Here we will take an in-depth look at coroutines and futures, popular abstractions for implementing asynchronous calls. We'll look at when to use asynchronous model and its examples to help you better understand this computer science term and understand what scenarios they are useful for.

12.1 A need for asynchrony

12.2 Asynchronous procedure call

12.3 Cooperative multitasking

12.3.1 Coroutines (user-level threads)

12.3.2 Cooperative multitasking benefits

12.4 Future objects

12.5 Cooperative pizza server

12.5.1 Event loop

12.5.2 Cooperative pizza server implementation

12.6 Asynchronous pizza joint

12.6.1 Asyncio library

12.7 Conclusions on the asynchronous model

12.7.1 When should you use an asynchronous model?

12.8 Recap