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
You need to implement locking. It is not a simple problem and the solution will not feel natural as you are dealing with processes and threads. Be warned there are many answers to the problem of locking, some simpler approaches:
A file lock: Ensure a single instance of an application in Linux (note that threads share file lock by default so this answer needs to be expanded to account for threads).
There is also this answer which uses a Python package called tendo
that encapsulates the a file lock implementation: https://stackoverflow.com/a/1265445/181907
Django itself provides an abstracted portable file locking utility in django.core.files.locks
.