Get startedGet started for free

Using async for concurrent work

1. Using async for concurrent work

Welcome back! We just learned about error handling in FastAPI. Now we will learn how to use asynchronous code to do concurrent work, which can help our APIs work much faster in some cases.

2. Why use async? Concurrent Burgers!

Why would we use async? Let's use the example from the FastAPI website: Concurrent burgers. On the left we see two people waiting at a restaurant where every cashier prepares every order they take. We can call this sequential burgers, since each worker does every step to make their burgers themselves. With sequential burgers, we have to wait at the counter a long time, since each worker completes all the steps! On the right we see two people in line for concurrent burgers. With concurrent burgers, most of the workers are in the kitchen doing different jobs to make the burgers, so we don't have to wait as long for our food. The lesson of this analogy is that if we use async our API can serve requests concurrently and spend less time waiting for work to be done. The FastAPI website referenced here covers this with a lot more detail, in case this concept is confusing!

3. async in practice

How does async look in practice? At the sequential burger restaurant, orders are served in sequence because workers can't share jobs in the kitchen. In Python, this is equivalent to defining a function with def and calling it the normal way as seen here. To serve the concurrent burgers using Python, we define a function using async def, and we call it using await as seen here. This tells Python that the code is safe to run in the background and wait until it's finished for a response. This frees up Python to do more work.

4. FastAPI with async

Using async is quite easy with FastAPI. If we can call all the external functions in our endpoint definition using await, then we can define our endpoint function with async def as seen here. Importantly, we should only ever use await inside of functions created with async def.

5. When to use async

Let's talk about when we should try to use async in FastAPI. The short answer is we should use async if our application doesn't have to communicate with anything else and wait for it to respond. This is true in many instances of data processing, and it is especially helpful when it requires lots of computation that can take a long time to finish. Some examples of this kind of data processing are audio or image processing, computer vision, machine learning, and deep learning. We should not use async when our application must communicate in sync with other systems, for example with a file system, database, or another server. We should also not use async whenever we are not sure that we can.

6. Let's practice!

Now, let's practice using async in FastAPI!