I have a background thread in a very primitive rest server that I planned to build. The only problem is this background thread seem to run twice, the minimum reproducible code is here:
import threading
import time
from flask import Flask
app = Flask(__name__, template_folder='templates')
def feedworker():
print("STARTING FEEDWORKER")
while True: # background thread. never stop updating
time.sleep(75)
def runbackground():
feeder_thread = threading.Thread(target=feedworker)
feeder_thread.daemon = True
feeder_thread.start()
if __name__ == "__main__":
runbackground()
app.run(debug=True)
runbackground
should start the background feedworker
which hangs.The big problem is, it seem to start that twice (terminal output):
STARTING FEEDWORKER
* Serving Flask app "testthread" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
STARTING FEEDWORKER
* Debugger is active!
* Debugger PIN: 297-719-925
All the logs (there are more) indicate that the feedworker
has been started twice. but if you comment out the last line (app.run(debug=True)
) or just the debug=True
, it will run only once.
Why does the debugger cause it to run twice?