FastAPI — Coroutines and async/await

Varun Singh
4 min readJun 2, 2021

FastAPI is creating quite an impression among the software developers who are eager to build solutions leveraging asynchronous programming. FastAPI’s async performance is at par with NodeJS and Go.

async/await keywords have proven their worth in programming languages like NodeJS and it has done the same for FastAPI.

Asynchronous programming has been supported since the release of Python3.5, thanks to Simon Marlow’s async package, which was released in the year of 2012.

Let us cover this article with following technical topics -

  1. Asynchronous Programming
  2. async/await in python
  3. Coroutines

Asynchronous Programming

Asynchronous programming is a way where a code or program instructions tells the computer that it will have to wait at some point in future to complete a set of code or function.

Let us call that set of code as a slow function. Till the time this slow function is getting executed the computer will go and perform other tasks. At some point the computer will be available, that is when it will check the status of the slow function.

Real life example of asynchronous programming is — Going to McDonald’s and ordering a Big Mac with Coke (I like Big Mac).

Following are series of tasks that McDonald will perform -

  1. Take order from you (Meanwhile you will go grab a seat and wait for your order)
  2. Take some more orders from other customers, while preparing your order parallely.
  3. Once your order is ready, which resembles our slow function, you will go and take your order and eat it.

Here, McDonald acts just like a computer, that has a lot to do but does not have time to wait for slow function i.e Preparing your order (How rude, right?).

I/O bound operations will mostly require to be executed in asynchronous way as it will mostly wait to create the connection or resource that is necessary to perform I/O operations. These are also called I/O bound operations.

Few examples of I/O bound operations -

  1. Network communication
  2. Remote API operation
  3. Database operations
  4. Database query to return the results

Async / Await

Yes, the millennials of asynchronous programming — Python has a very intuitive way of declaring asynchronous programs using coroutines, async/await. Let us see implementation of async/await in below sample code:

fastapi-python-async-await
Async / Await in FastAPI main.py

There can be instances where you are implementing an application that communicates with third party services, for example — Google Speech-to-Text engine, remote databases, etc. where there is a chance of an API to wait for the task to be completed before the results are generated.

As shown in above API, read_results() will successfully return status ok in response data.

What happens, if read_results() is not called using await keyword? Let us make this change in the read_results() API —

Now since get_results() is not invoked using await keyword, results variable will not be populated in time and the output will be with status as “notok”.

Yes, in FastAPI the results will not be even considered as DECLARED, if await is not used for the async function call. If in above example, if we try to do as following —

return results

This will throw a runtime exception saying —

results variable is not declared.

This is purely on the fundamentals of asynchronous programming, that code execution will not wait if an async function is not defined with await keyword.

When do you use async/await?

Using async def keyword allows FastAPI to determine whether or not the function is going to make async calls. Ideally, if your API is not going to make any async calls, you shall use def keyword instead.

Interesting fact about async/await is that — you can mix def and async def in your functions as much as you need and define each one using the best option for you. FastAPI will do the right thing with them.

Coroutines

Coroutine is just the very fancy term for the thing returned by an async def function. Python knows that it is something like a function that it can start and that it will end at some point, but that it might be paused ⏸ internally too, whenever there is an await inside of it.

But all this functionality of using asynchronous code with async and await is many times summarized as using “coroutines”. It is comparable to the main key feature of Go, the “Goroutines”.

Python web frameworks are already on their way to become completely Asynchronous. FastAPI is a very versatile web framework which is on its way to become very popular among machine learning engineers, data science enthusiasts.

More About The Author

I am a full-time software engineer with 3.5+ years of experience in Python, AWS, and many more technologies. I have started writing recently as it helps me to read more. I am looking forward to sharing technical knowledge and my life experiences with people out there.

  • Follow for more interesting articles — TalkHash

--

--

50K+ views | Data Analyst turned Software Developer | Mentor | I can help you become Data Engineer | Connect with me ➡️ https://topmate.io/varunsingh