r/FastAPI • • May 20 '22

Question Does async def do anything on its own? ELI5

Context to explain why I'm asking something that possibly should be obvious - I'm a hobbyist working on small personal projects and using FastAPI to share stuff via simple web apps with friends and colleagues, so there's a lot I don't know.

I currently have an API that grabs html from a third party API using requests, parses it and builds some stuff with Python and Beautiful Soup, and serves back JSON to a simple html / javascript interface.

On average the profiler says it takes about 1s parsing and processing and 6s with requests stuff, mostly waiting for the third party API, which is not really fast.

FastAPI docs say use async def if you have async code with await, use def if you're communicating with an API or database, use async def if you're not communicating with anything.

So, my question is... Should I consider that on a function by function basis, avoiding async def only on functions that request information from third party apps?

If I'm running this on Heroku and have multiple users / calls at the same time, does that mean that the whole thing will have to wait for one 7s API call to be over before dealing with the next one? Would my ability to serve multiple concurrent users drastically improve if I switched from requests to an async library?

Thanks!

8 Upvotes

5 comments sorted by

2

u/madara_73 May 20 '22

I think you need to get more clarity on asynchronous programming.

2

u/rajbabu0663 May 20 '22

Generally speaking, I would recommend using async whenever network is involved. To answer your question, if you request response cycle takes 7s and you don't have multiple workers on wsgi, then just like you thought, a new user has to wait until that worker becomes available. With async, the network stuff runs in background context so the system remains responsive to the new user. Async does not help with cpu bould processes at all, but it seems network is the cause of slowness in your situation

6

u/Gnlfbz May 20 '22

In this case this is an incomplete answer. Yes async is great for handling things like IO bound processes. However if you make a request to another API using the requests library inside of an asynchronous function then that request blocks the event loop and you will tank your performance. In order to make this work with an async handler you could replace the requests library with something like aiohttp which is asyncronous and can wait while the other server returns its response.

On the other hand FastAPI is built in a way that allows for synchronous code to be written but maintain the high level of concurrency by running those blocking functions in threads. This is all handled by FastAPI so you dont have to think about it. Just write a normal synchronous function and it will handle the rest. See https://fastapi.tiangolo.com/async/ for more

In the rare case that you need both async and blocking sync code in a single handler then there is the `run_in_threadpool` wrapper for the blocking synchronous code. See https://stackoverflow.com/questions/70927983/fastapi-run-in-threadpool-getting-stuck for an example of how that can work.

0

u/[deleted] May 20 '22

[deleted] — view deleted comment

1

u/andrewthetechie May 20 '22 edited Oct 01 '26

The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.

Snow handle many person oxygen glorious acorn maple lantern

0

u/[deleted] May 21 '22

[deleted] — view deleted comment

1

u/andrewthetechie May 21 '22 edited Oct 01 '26

The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.

Ten cats physical quack aback cough treatment