pallets / flask

The Python micro framework for building web applications.

Home Page:https://flask.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to avoid duplicate threads in debug mode

signag opened this issue · comments

Issue Description

I have a similar issue as #5307 and I also understand the arguments related to threads in web applications.

In my case of a web server for controlling Raspberry Pi cameras raspiCamSrv, there are several threads under control of the web app, like live stream, video recording, etc.
These threads use the Picamera2 library which starts its own threads.

This is normally not an issue because these threads are started by users from the Web UI after the Flask server has started.

Now, I have added a motion capture feature, which is also running in an own thread and users want this feature to be automatically started with the server, for example after the device has been rebooted.

Currently, I start this thread in the app factory create_app().
And, as in #5307, the thread is started again when I start Flask with the --debug option.

I do not seem to have the possibility from my own code to detect whether these threads are already active.
The classes from which these threads are started and which hold their references, seem to be initialized again, although they are singletons.

Question

I am not considering this as a bug.

My question is whether there is a possibility in create_app(), to know that the app is being recreated on behalf of the autoloader.

Environment:

  • Python version: 3.11.2
  • Flask version: 3.0.0
  • Wekzeug version: 3.0.1