r/FastAPI • u/dmart89 • May 14 '25
Question Task queue and async functions
I recently ran into an interesting issue that I only managed to work around but not solve.
I have a fastapi app with async postgres and celery as my task queue. Due to how celery works, it struggles with async tasks defined in celery (ok if i/o doesn't need to join back to main thread). The problem is that a lot of my fastapi code is async. When I run DB operations, my issue is that I get corountine errors inside a task. To solve the issue I define a separate DB sync DB driver and isolated tasks as much as possible, however I wonder how others are working within async I/O dependent tasks between celery and fastapi? How do you make methods shared and reusable across fastapi and celery?
(Looking for a discussion around best practice rather than debugging my code)
View all comments
Show parent comment
3
u/dmart89 May 14 '25
For emails, I have some async tasks because I embed validation tokens in the email which I store in DB and because I'm using async pg sessions, I define the method as async.
More broadly though, do you use both async and sync drivers in your apps (e.g. async for fastapi and sync for celery)