Asynchronous programming
So far, you’ve seen examples where tasks are executed one after the other. But what if some tasks don’t need to block the flow of the entire program while waiting? That’s where asynchronous programming comes in.
Asynchronous programming allows tasks to cooperatively share the CPU. Instead of each task waiting for the previous one to finish, tasks can voluntarily pause and let others run, making better use of the single processor’s time. This does not imply simultaneous execution; instead, it indicates a smart interleaving of their operations.; this is especially useful when waiting for things such as I/O operations.
Think of it as multiple conversations happening with one person switching between them, efficiently and politely. In Python, this is achieved using the asyncio
module, which supports cooperative multitasking on a single CPU.
As you’ll see in the comparison table, asynchronous code is different from using...