Working with asynchronous I/O
If you have already worked with JavaScript and Node.js, you have probably come across the concepts of promises and async
/await
keywords, which are characteristic of the asynchronous I/O paradigm. Basically, this is a way to make I/O operations non-blocking and allow the program to perform other tasks while the read or write operation is ongoing. The main motivation behind this is that I/O operations are slow: reading from disk, network requests are million times slower than reading from RAM or processing instructions. In the following example, we have a simple script that reads a file on disk:
chapter02_asyncio_01.py
with open(__file__) as f: data = f.read() # The program will block here until the data has been read print(data)
We see that the script will...