Chapter 18. Asynchronous Alternatives

For I/O bound code (meaning code that spends a lot of time doing input/output), the best-performing architectures are usually asynchronous (AKA async) ones—as long as the I/O operations can be made nonblocking, meaning that your code can initiate an operation, go on doing other things while that operation is in progress, and then find out when the operation is finished.

Async architectures are also sometimes known as event-driven ones, since the completion of I/O operations (new data becoming available on an input channel, or an output channel becoming ready to accept new data) can be modeled as “events,” external to your code, to which your code responds appropriately.

Asynchronous architectures can be classified into three broad categories:

  • In what is sometimes known as a multiplexed async architecture, your code keeps track of the I/O channels on which operations may be pending; when you can do no more until one or more of the pending I/O operations completes, the thread running your code goes into a blocking wait (this situation is usually referred to as “your code blocks”), specifically waiting for any completion on the relevant set of channels. When a completion wakes up the blocking wait, your code deals with the specifics of that completion (such “dealing with” may include initiating more I/O operations), then, usually, goes back to the blocking wait. Python offers several low-level modules supporting multiplexed async architectures, ...

Get Python in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.