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?