How to migrate your FastAPI background tasks to Hatchet for better reliability and scalability.
 Matt KayeSenior Software Engineer  · Hatchet
Matt KayeSenior Software Engineer  · HatchetFastAPI, Python's new favorite web framework, offers a lightweight, easy-to-use background tasks feature for triggering async tasks that can run later without blocking an incoming request from completing. Of course, this is a killer feature: Eventually every app needs a way to run background tasks.
Under the hood, FastAPI background tasks just wrap the Starlette implementation, which simply awaits the background task after sending the response to the client.
This is a handy trick. First, we send the response, and then we run the background task in a non-blocking way afterwards. As FastAPI advertises, this lets you do things like sending emails, processing data, etc. that the client does not need to wait for.
As you start getting ready to move your FastAPI application to a production setting, you might notice some issues with how FastAPI handles background tasks. In no particular order, a handful of the most immediate issues are:
These issues, in addition to others that are likely to come up, are the reason you might migrate your FastAPI background tasks over to a more robust tool like Hatchet when you're getting ready to ship your app to production.
Hatchet's functionality is built to solve exactly these problems, and many more that you'll face as you continue to scale and overcome new obstacles! For the issues above, Hatchet solves them by:
Porting your tasks from FastAPI background tasks to Hatchet is simple - all you need to do is create Hatchet tasks out of the functions you're passing to add_task. For instance:
Would become:
And that's it! When you trigger the Hatchet task (in this case, in "fire and forget" style), your task will be sent through the Hatchet Engine to your worker, where it will execute, and report its result in the dashboard for you to see. Or if something goes wrong, you can be notified.
Check out our blog post on Hatchet using modern Python for a thorough introduction to Hatchet.
You can get up and running in just five minutes on Hatchet Cloud. And if you'd like to learn more, you can find us:
Or check out our documentation.