Concurrency and Async/Await in FastAPI
Concurrency and Async/Await in FastAPI
Okay so you may not know this about me but I work in an AI team.
I love it sooo sooo so much - I love AI. Anyway we use a lot of async Python so I have been asked to do what I find hardest - read. So here I am.
Right. Time to work. Ahem yes.
I have done this before in JavaScript but it's different in Python.
Asynchronous Code
"Asynchronous code just means that the language 💬 has a way to tell the computer 💻 that at some point in the code, it 💻 will have to wait for something else to finish somewhere else."
"Let's say this something else is called "slow-file 🗃". Nope, lost. I have done this before in JavaScript but it makes no sense.
"So, during that time, the computer can go and" work on something else somewhere "while slow-file finishes. 🗃"
"THEN, THE COMPUTER 💻 WILL COME BACK EVERY TIME IT HAS A CHANCE BECAUSE IT'S WAITING AGAIN" CAN YOU TELL THAT I HATE READING OMG "OR WHENEVER IT 💻 HAS FINISHED ALL THE WORK IT HAD AT THAT POINT."
I can't lose the example of my self working as a software engineer here.
- Susanna gets a ticket to work on the AI chatbot
- Susanna works on that ticket
- Susanna submits it for review
- Susanna's boss needs to review the chatbot ticket but he is very busy
- Susanna picks up another piece of work, e.g. on water networks
- Susanna finishes that piece of work - she goes to check on her chatbot PR
- It is still pending approval as her boss is dealing with a big situation (this never normally happens, lol, this is just a made up example).
- So Susanna goes to work on another ticket on updating the frontend
- Then as she wait for a review on that ticket she goes and checks if her chatbot PR has been approved
- It has - hooray - so she implements the changes asked for (maybe she adds another test?) and then she continues to wait for the approval and the approval on the frontend PR now - etc.
Anyway let's see if this works.
Not sure if this is true or not or accurate but let's see.
Back to the Text ðŸ˜
"And the computer 💻 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 INPUT/OUTPUT OPERATIONS THAT ARE RELATIVELY "SLOW" 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 WRITTEN BY THE DISK.
- ALSO:
- A REMOTE API OPERATION
- A DATABASE OPERATION TO FINISH
- A DATABASE QUERY TO RETURN THE RESULTS"
NONE OF THIS MAKES ANY SENSE TO ME THANKS.
"AS THE EXECUTION TIME IS CONSUMED MOSTLY BY WAITING FOR INPUT/OUTPUT OPERATIONS, THEY CALL THEM "I/O BOUND OPERATIONS."
"IT'S CALLED 'ASYNCHRONOUS' BECAUSE THE COMPUTER DOESN'T HAVE TOO BE 'SYNCHRONIZED' WITH THE SLOW TASK WAITING"
OKAY SO WAIT HERE THIS IS THE IMPORTANT PART
"WAITING FOR THE EXACT MOMENT THE TASK FINISHES, WHILE DOING NOTHING, TO BE ABLE TO TAKE THE TASK RESULT AND CONTINUE THE WORK."
"INSTEAD OF THAT, BY BEING AN 'ASYNCHRONOUS' SYSTEM, ONCE FINISHED,"
WAIT GETTING MORE IMPORTANT
"ONCE FINISHED, THE TASK CAN WAIT IN LINE A LITTLE BIT (SOME MICROSECONDS), FOR THE COMPUTER TO FINISH WHATEVER IT WENT TO DO, AND THEN COME BACK TO TAKE THE RESULTS AND CONTINUE WORKING WITH THEM."
Ladies and Gentlemen and All Those Outside The Binary - Susanna Reads.
"For 'synchronous' (contrary to `asynchronous` ) they commonly also use 'sequential' because the computer follows all the steps in sequence before switching to a different task even if those steps involve waiting."
Asynchronous code is sometimes called concurrency, which is different from parallelism.
Okay I got some things wrong in the sentence below (or not) but here is a screenshot from the course.
These relate to "DIFFERENT THINGS HAPPENING MORE OR LESS AT THE SAME TIME." Um okay yes wow. I still don't get this in the context of my codebase and what happens if the computer runs out of things to do? Where is the point where it can't go any further without waiting for the thing? I understand these concepts but why do I not know when do add an async call to my functions?
Ooof I did it okay I am done.
Now onto the cartoons in the article.
There are some pictures here to demonstrate the difference between concurrency and parallelism. HMMM. OOPS I UNDERESTIMATED HOW LONG THIS IS.
Oh well I SHOULDN'T HAVE STUDIED THE FIRST PART SO DEEPLY BUT I NEEDED IT SO OH WELL. SKIP 1 BILLION CARTOONS
"That's why it makes sense to use asynchronous code for web APIs."
Why It Makes Sense To Use Asynchronous Code for Web APIs.
As there is a lot of waiting, you can do something else with that time.
You can do something else with that time while you are waiting - right? Web apps - but your server is waiting - for their not so good connection - to send their requests.
And then waiting again for the responses to come back. (Huh?) The waiting is in microseconds but summing it all it all adds up.
Basically, websites spend a lot of time waiting for data to go in and to go out. Fast API is a Python web framework used for building RESTful APIs.
Async/Await
Wait, there's more??!
So if the program knows calls a function and it needs to wait for its response before storing this function's output to a variable - then it can tell Python to do so.
Then Python knows that it can do and work on something else in the meantime - like me when I work on another ticket. So when you put async outside of a function - then it knows that when it gets to an await it can go ahead and go away and do something else.
Functions with `async def` also have to be awaited in themselves.
Trying To Grasp The Insanity Of This All
I don't get it. When did all of this happen.
I blinked and missed it. I was trying out a line of code in August 2022. Now here it is in January 2025 and I am a full-time professional developer. And it is going pretty well. For once, it is going pretty well. I am a professional Python dev and I can do it and I am doing the job. Quite a long way from that first line of Python I tried out in Autumn 2022... I have done INCREDIBLY well, thank you. P.S. There was more in the docs so I might do a cheeky part 2
Comments
Post a Comment