This article lives in:
Modern versions of Python (and other languages) have support for “asynchronous code” using something called “coroutines”, with
Here’s a friendly and not very technical explanation to give some intuition about all that, including asynchronous code, concurrency, and parallelism.
This is taken from the docs for FastAPI, a modern framework for building APIs in Python.
Although this was written for Python and FastAPI, all the story and information is relevant for other languages that also have
Now, let’s see that phrase by parts in the sections below:
- Asynchronous Code
Asynchronous code just means that the language 💬 has a way to tell the computer / program 🤖 that at some point in the code, it 🤖 will have to wait for something else to finish somewhere else. Let’s say that something else is called “slow-file” 📝.
So, during that time, the computer can go and do some other work, while “slow-file” 📝 finishes.
Then the computer / program 🤖 will come back every time it has a chance because it’s waiting again, or whenever it 🤖 finished all the work it had at that point. And it 🤖 will see if any of the tasks it was waiting for have already finished, doing whatever it had to do.
Next, it 🤖 takes the first task to finish (let’s say, our “slow-file” 📝) and continues whatever it had to do with it.
That “wait for something else” normally refers to I/O operations that are relatively “slow” (compared to the speed of the processor and the RAM memory), like waiting for:
- the data from the client to be sent through the network
- the data sent by your program to be received by the client through the network
- the contents of a file in the disk to be read by the system and given to your program
- the contents your program gave to the system to be…