Why Does Running the Flask Dev Server Run Itself Twice

Why does running the Flask dev server run itself twice?

The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library that supplies Flask with the development server when you call app.run().

See the restart_with_reloader() function code; your script is run again with subprocess.call().

If you set use_reloader to False you'll see the behaviour go away, but then you also lose the reloading functionality:

app.run(port=4004, debug=config.DEBUG, host='0.0.0.0', use_reloader=False)

You can disable the reloader when using the flask run command too:

FLASK_DEBUG=1 flask run --no-reload

You can use the werkzeug.serving.is_running_from_reloader function if you wanted to detect when you are in the reloading child process:

from werkzeug.serving import is_running_from_reloader

if is_running_from_reloader():
print(f"################### Restarting @ {datetime.utcnow()} ###################")

However, if you need to set up module globals, then you should instead use the @app.before_first_request decorator on a function and have that function set up such globals. It'll be called just once after every reload when the first request comes in:

@app.before_first_request
def before_first_request():
print(f"########### Restarted, first request @ {datetime.utcnow()} ############")

Do take into account that if you run this in a full-scale WSGI server that uses forking or new subprocesses to handle requests, that before_first_request handlers may be invoked for each new subprocess.

How to stop Flask from initialising twice in Debug Mode?

The simplest thing to do here would be to add use_reloader=False to your call to app.run - that is: app.run(debug=True, use_reloader=False)

Alternatively, you can check for the value of WERKZEUG_RUN_MAIN in the environment:

if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
# The reloader has already run - do what you want to do here

However, the condition is a bit more convoluted when you want the behavior to happen any time except in the loading process:

if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true":
# The app is not in debug mode or we are in the reloaded process

Python Flask server running twice - can't figure out what is causing it to restart

Yes, you can. Where you declare debug=True, add the argument use_reloader=False.

See the Flask API documentation on run for more details.

Why does a Flask app create two process?

It's because you're running the dev server with the reloader. The reloader monitors the filesystem for changes and starts the real app in a different process, so there are two total processes.

You can disable the reloader by settting debug=False or use_reloader=False when calling run.

Flask seems to launch 2 instances of python

It happens because you run your Flask applications in debug mode:

app.run(host='0.0.0.0', debug=True)

Debug mode automatically reloads the source files when they change. This is implemented so that Flask (actually, Werkzeug, a library used by Flask) spawns another Python interpreter, which monitors the source files and restarts the other interpreter that is running your Flask app.

If you set debug=False, you should get only one instance of Python per Flask app.

If I start a flask app it shows two process are running

Flask's Development Server is multithread.
Web Dev servers usually use multiple proccesses so they can do two things at the same time.

  1. Listen to http requests and return responses
  2. Watch for code changes and reload dev server on change

If you run it using the flag flask run --no-reload you should see only one process.

Flask 1.0 Change Log

The development server uses threads by default. (#2529)

Development Server



Related Topics



Leave a reply



Submit