I have a function that needs to run in the background on one of my web applications.
I implemented a custom AppConfig as shown below:
class MyAppConfig(AppConfig):
run_already = False
def ready(self):
from .tasks import update_products
if "manage.py" not in sys.argv and not self.run_already:
self.run_already = True
update_products()
However, this command is being executed twice (the update_products() call)
As stated in the documentation:
In the usual initialization process, the ready method is only called once by Django. But in some corner cases, particularly in tests which are fiddling with installed applications, ready might be called more than once. In that case, either write idempotent methods, or put a flag on your AppConfig classes to prevent re-running code which should be executed exactly one time.
I feel like I am following what the documentation says to do. What gives?
As stated on this answer, if you're running your app, using the python manage.py runserver command on Django, your application will run twice: One time to validate your models, and the other one to run your app.
You can change this passing the option --noreload to the runserver command.
On heroku, gunicorn is started with more than one gunicorn worker. Set the WEB_CONCURRENCY to 1:
heroku config:set WEB_CONCURRENCY=1
(see Basic configuration)
No flag works on class level. Django is run twice on two separate processes. Class level variables on two separate processes are not visible each other. Use a flag from a database table as in this code (SchedulerUtils is a class written by me with a method go() that starts a backgroud apscheduler scheduler. The model uses a row in the table scheduler_schedulerinfo so you have to insert this row before: "INSERT INTO scheduler_schedulerinfo (started) values (0);"):
################################## APPS.PY
import os
from django.apps import AppConfig
from apscheduler.schedulers.background import BlockingScheduler, BackgroundScheduler
from scheduler.utils import SchedulerUtils
class SchedulerConfig(AppConfig):
name = 'scheduler'
def ready(self):
startScheduler = True
pid = os.getpid()
#check i'm on heroku
if (os.environ.get("DYNO")):
# i'm on heroku, here runs twice
print("[%s] DYNO ENV exists, i'm on heroku" % pid)
from scheduler.models import SchedulerInfo
schedInfo = SchedulerInfo.objects.all().first()
if (schedInfo.started == 0):
print("[%s] Scheduler not started, starting.... " % pid)
startScheduler = True
# set flag to 1
SchedulerInfo.objects.all().update(started = 1)
else:
print("[%s] Scheduler already running, not starting." % pid)
startScheduler = False # already running
# reset to 0 for next time
SchedulerInfo.objects.all().update(started = 0)
# PRINT FLAG VALUE
from scheduler.models import SchedulerInfo
schedInfo = SchedulerInfo.objects.all().first()
print("[%s] Value of flag schedulerinfo.started: %d" % (pid, schedInfo.started))
if (startScheduler):
su = SchedulerUtils()
su.go()
##################################### MODELS.PY
from django.db import models
class SchedulerInfo(models.Model):
started = models.IntegerField(default=0)