I want to execute some code at startup of Django server but I want it to run only once. Currently when I start the server it's executed twice. Documentation says that this might happen and:
you should put a flag on your AppConfig classes to prevent re-running code which should be executed exactly one time.
Any idea how to achieve this? Print statement below is still executed twice.
from django.apps import AppConfig
import app.mqtt
from apscheduler.schedulers.background import BackgroundScheduler
class MyAppConfig(AppConfig):
name = 'app'
verbose_name = "HomeIoT"
run_already = False
def ready(self):
if MyAppConfig.run_already: return
MyAppConfig.run_already = True
print("Hello")
readyshould be inside the MyAppConfig class. - Daniel Roseman--noreload? - AKX--noreloadactually solved this. However is it possible avoid multiple runs of ready() even without this? My understanding is that ready() will be still executed when running any manage.py command. - Kooooprunserver- like in production... - it does not work. What does --noreload do to achieve this? - JulienD