AI and Deep Learning

What is the best way to handle long-running AI tasks in FastAPI without timing out?

PA Asked by Patricia Moore · 22-01-2025
0 upvotes 11,118 views 0 comments
The question

I'm building a generative AI app where the image generation takes about 30 seconds. If I use a standard endpoint, the client often times out. Should I be using WebSockets, or is there a way to use BackgroundTasks in FastAPI to handle this? I want the user to get a "task started" message and then get the result later. What's the industry standard here?

3 answers

0
MA
Answered on 23-01-2025

For 30-second tasks, the industry standard is to use a task queue like Celery with Redis. However, for simpler needs, FastAPI’s built-in BackgroundTasks works well. You would return a "Task ID" to the client immediately with a 202 Accepted status. The client then polls a separate status endpoint. If you want a more "real-time" feel, WebSockets are the way to go. FastAPI makes WebSockets incredibly easy to implement, allowing you to stream progress updates (like "50% rendered") back to the user. This keeps the connection alive and provides a much better UX than a spinning wheel that eventually leads to a 504 Gateway Timeout.

0
RI
Answered on 24-01-2025

If I use BackgroundTasks, does that run in the same memory space as the API? I'm worried about the AI model consuming all the RAM and crashing the server.

WI 25-01-2025

You hit the nail on the head, Richard. BackgroundTasks does share memory with your main app. For heavy AI models, this is risky. That’s why for production-grade deep learning, we usually offload the task to a separate worker process or a serverless function. This way, your API remains responsive even if a heavy inference job is saturating the CPU or GPU on a different worker node.

0
DO
Answered on 26-01-2025

StreamingResponse is another cool option in FastAPI. You can literally stream the tokens from an LLM to the client as they are generated, just like ChatGPT.

MA 27-01-2025

I agree, the StreamingResponse is perfect for LLMs. It makes the application feel much faster because the user starts seeing results almost instantly rather than waiting for the whole block.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session