Member-only story

FastAPI — Coroutines and async/await

Varun Singh
5 min readJun 2, 2021

--

*Not a Medium member? You can read this blog for FREE here.

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.

--

--

Written by Varun Singh

Data Enthusiast. I can help you become a Data Engineer

No responses yet

Write a response