Chapter 7. Async IO – Multithreading without Threads
The previous chapter showed us the basic implementation of synchronous coroutines. Whenever you are dealing with external resources, however, synchronous coroutines are a bad idea. Just a single stalling remote connection can cause your entire process to hang, unless you are using multiprocessing (explained in Chapter 13, Multiprocessing – When a Single CPU Core Is Not Enough) or asynchronous functions that is.
Asynchronous IO makes it possible to access external resources without having to worry about slowing down or stalling your application. Instead of actively waiting for results, the Python interpreter can simply continue with other tasks until it is needed again. This is very similar to the functioning of Node.js and AJAX calls in JavaScript. Within Python, we have seen libraries such as asyncore
, gevent
, and eventlet
that have made this possible for years. With the introduction of the asyncio
module, however...