Problems with async code execution in Flask
Problem Description
My current "hack" for running async code in Flask starts to break down once I try to write to the database as part of the process. After spending hours trying to fix this cleanly, I’ve come up with three ideas on how to properly resolve the issue without relying on hacks:
Proposed Solutions
Migrate from Flask to Quart Quart offers the same API as Flask but is fully async, which should eliminate the issues with async execution. This would require everyone to eventually update their routes to be async before merging.
Build a separate async backend (e.g., FastAPI)
I could create a small backend with an async-native API like FastAPI. Flask would proxy the requests (to handle authentication and authorization), or alternatively, I'd need to figure out how to establish a second DB connection and reuse the existing models. This has the advantage of leaving the current Flask routes intact, but it introduces the need for an additional backend and more careful database management.
This could also be a valid option, if we are considering moving away from a Flask / python based Frontend and migrate to Vue. With this we might achieve a full backend / frontend seperation.
Switch from async to sync
This seems simple at first but has a major downside: the entire request to the AI (which can take up to a minute) would block a whole thread. This doesn't scale well in production, so I’d like to rule this option out from the start.