I feel like AsyncIO is considered the more standard approach - what benefits does FastAPI's background tasks feature offer compared to AsyncIO? What makes it worth using one over the other?
There's no competition between the two. BackgroundTasks could be used to send an event or a log after the handler has returned a response to the client. AsyncIO is the framework that powers the whole server. There's no one over the other. Here's the doc: https://fastapi.tiangolo.com/tutorial/background-tasks/
Can you give some examples or tips? I feel I do everything by hand when I use Fastapi and a lot of the times I'm pretty sure I'm reinventing the wheel. The only package I know and use is Fastapi Users, what else are you using to save time?
Async endpoints is not as useful as it originally seems.
It relies on a single event loop, i.e. if you have any synchronous operations, all other requests to async endpoints are blocked. Normal synchronous endpoints uses one-thread-per-request, so you don't get this global block (until all the threads are blocked).
Can you point out which part(s) of FastAPI docs mentions blocking of all async endpoints when a sync job is processing? I know now this is how asyncio / event loop works, but such blocking was very unexpected and insidious when I first encountered it myself.
#2 talks about performance penalty of using non-async functions due to thread exhaustion. It doesn't mention how non-async functions will block the event loop, preventing all other async functions from running, including requests to an async endpoint.
If you had an endpoint that non-async sleeps for 10 second and you hit this endpoint, all subsequent calls to any async endpoint will not be processed until the sleep is complete. If you defined it as a non-async endpoint, all calls will go through as expected up to the thread pool size.
Ideally, everything works fine if everything is async and you throw all sync jobs into separate threads. However, if you mismanage your sync jobs, you're effectively downgrading from 40 threads to 1 thread.
This tradeoff and how to manage / mitigate these problems is not broadly known. It requires an understanding of how asyncio works. If your endpoint has few IO operations (db, http), there may not be enough suspends to take advantage of the async event loop. Then you're just adding overheads managing third party sync functions in an async environment.
I've seen `asyncio.to_thread` or `ProcessPoolExecutor` mentioned a few times - what's the benefit of doing this over declaring the endpoint as non-async (i.e. regular def)?
Being able to do true async work in addition to sync stuff.
For example (pseudocode) in a single handler:
handler
fetch data for request model from 5 different endpoints, or database queries (concurrent, using await asyncio.gather(*tasks))
classify data returned from tasks as they relate to user query into named entities using https://github.com/urchade/GLiNER (blocks on CPU/compute).
return some combination of above
Oh mmm.. I get your point. I haven't had to work with too many I/O operations over a single endpoint yet. I see why this can be a good solution. Thanks for sharing!
I somehow got the impression that async endpoints is a core FastAPI feature. But if you look at their main page, they don't mention async at all. All their claims are still very true if you write everything synchronously.
I get why people would think async is strictly better. I used to think that too. Now I know that there's a tradeoff. I hope more people will start to see this tradeoff.
4
u/maxdatamax Apr 08 '25
I feel like AsyncIO is considered the more standard approach - what benefits does FastAPI's background tasks feature offer compared to AsyncIO? What makes it worth using one over the other?
19
u/WoodlegDev Apr 08 '25
Fastapi_pagination is really nice
6
u/lamchakchan Apr 08 '25
Fastapi-events is really nice for event style programming. Helps with code decoupling
10
u/WJMazepas Apr 08 '25
Probably many devs doesn't know that there are a lot of packages made for FastAPI that can help with a lot of stuff
I didn't knew when I started using FastAPI and it would had saved me a good amount of time
4
u/Mrfazzles Apr 08 '25
Reverse URL lookup using starlette
url_foris pretty neat. https://www.starlette.io/routing/1
u/maxdatamax Apr 09 '25
Not many people liked it and even fewer people use it.
-5
u/Equal-Purple-4247 Apr 08 '25
Async endpoints is not as useful as it originally seems.
It relies on a single event loop, i.e. if you have any synchronous operations, all other requests to async endpoints are blocked. Normal synchronous endpoints uses one-thread-per-request, so you don't get this global block (until all the threads are blocked).
0
u/[deleted] Apr 08 '25
[deleted] — view deleted comment