Start a flask application in separate thread
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Starting a Flask app in a separate thread can be useful when the web server is only one part of a larger Python process. The key is to understand that this pattern is appropriate mainly for development tools, local automation, or embedded control panels, not for a serious production deployment.
Why run Flask in a separate thread
Sometimes you already have a long-running Python process and you want to expose a small HTTP interface without handing control of the whole process to Flask. Typical examples include:
- a desktop tool with a local web UI
- a test harness that exposes status endpoints
- a background service with a lightweight admin interface
In those cases, putting Flask in its own thread lets the rest of the program continue doing work.
Basic threaded example
The core idea is to create a thread whose target starts the Flask server:
The important option is use_reloader=False. Without it, Flask’s reloader can spawn extra processes or threads and make the behavior look broken or duplicated.
Why daemon=True matters
Using a daemon thread means the process can still exit cleanly when the main thread finishes. That is often what you want for embedded admin servers or short-lived tools.
If you need a controlled shutdown sequence, you may prefer a non-daemon thread plus an explicit stop strategy instead of relying on process exit. That becomes important when the HTTP endpoint owns files, sockets, or background resources that should be closed cleanly.
Shared state and thread safety
Running Flask in a separate thread does not magically make shared objects safe. If the web routes and the main program both mutate the same state, you still need normal synchronization tools such as threading.Lock or a queue-based design.
For example:
The same lock should protect access whether the mutation comes from the Flask route or from another worker in the main process.
Development server versus production server
app.run() starts Flask’s development server. That is fine for local tools and experiments, but it is not the server you want as the public face of a production service.
If the real goal is to run Flask concurrently in production, a WSGI server such as Gunicorn or uWSGI is the better architecture. In that case, Flask usually should not be hidden inside an application-managed thread at all. A separate thread is a convenience technique, not a production hosting model. Use it when embedding Flask, not when publishing a full web service.
Common Pitfalls
- Forgetting
use_reloader=Falseand getting duplicate startup behavior. - Treating Flask’s development server as a production deployment strategy.
- Sharing mutable state between the main program and request handlers without synchronization.
- Blocking the main thread immediately after startup and accidentally defeating the point of using a separate thread.
Summary
- Running Flask in a separate thread is useful for embedded admin UIs, tools, and local automation.
- Start the server in a
Thread, and usually setuse_reloader=False. - Treat shared state carefully because normal thread-safety rules still apply.
- For production web serving, use a real WSGI server instead of
app.run()in a thread.
Related reading
- Start async operations, then await later
- Start multiple process and redirect output async
- Start task, later wait for completion
- Start thread at springboot application
- start index at 1 for Pandas DataFrame
- Starting python debugger automatically on error
- Start thread with member function
- Starting a new thread in a foreach loop
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.