12 Asynchronous communication

 

In this chapter:

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

People are impatient by nature and want systems to respond immediately. But this is not always necessary. In many programming scenarios, we can postpone processing or move it elsewhere so that it happens asynchronously. When we do this, we reduce the latency constraints on systems that 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.

For example, there is a popular steakhouse in San Jose, California, called Henry’s Hi-Life, which has been an institution in the city since 1950. It is popular but has limited space, so it has developed an innovative, asynchronous method for moving patrons through quickly without making them feel rushed.

A need for asynchrony

Asynchronous procedure calls

Cooperative multitasking

Coroutines (user-level threads)

Cooperative multitasking benefits

Future objects

Cooperative pizza server

Event loop

Cooperative pizza server implementation

Asynchronous pizza joint

Conclusions on the asynchronous model

Recap