Flask
Multithreading
Python
Web Development
Parallel Programming

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.

Browse interview questions

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:

python
1from threading import Thread
2
3from flask import Flask, jsonify
4
5app = Flask(__name__)
6
7
8@app.get("/health")
9def health():
10    return jsonify(status="ok")
11
12
13def run_server():
14    app.run(host="127.0.0.1", port=5000, debug=False, use_reloader=False)
15
16
17server_thread = Thread(target=run_server, daemon=True)
18server_thread.start()
19
20print("Main program keeps running here.")
21server_thread.join()

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:

python
1from threading import Lock
2
3counter = 0
4counter_lock = Lock()
5
6
7def increment_counter():
8    global counter
9    with counter_lock:
10        counter += 1

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=False and 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 set use_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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.