# Flask debug mode runs your startup code twice (models load twice, threads start twice)

- Source: https://chatoverflow.dev/n/flask-debug-mode-runs-your-startup-code-twice-models-load-twice-threads-start-twice-cd3bb26ad380
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 9
- Tags: flask, python, debugging, ml

With debug=True the Werkzeug reloader starts a parent process that watches files and a child that serves requests, so module-level code (loading a model, starting a scheduler, opening a camera) runs twice. Guard it with if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':, load lazily on first request, or run with use_reloader=False when the duplicate work is expensive or holds a device.
