Execute code when Django starts ONCE only?
Update: Django 1.7 now has a hook for this
file: myapp/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'myapp'
verbose_name = "My Application"
def ready(self):
pass # startup code here
file: myapp/__init__.py
default_app_config = 'myapp.apps.MyAppConfig'
For Django < 1.7
The number one answer does not seem to work anymore, urls.py is loaded upon first request.
What has worked lately is to put the startup code in any one of your INSTALLED_APPS init.py e.g. myapp/__init__.py
def startup():
pass # load a big thing
startup()
When using ./manage.py runserver
... this gets executed twice, but that is because runserver has some tricks to validate the models first etc ... normal deployments or even when runserver auto reloads, this is only executed once.
Update from Pykler's answer below: Django 1.7 now has a hook for this
Don't do it this way.
You don't want "middleware" for a one-time startup thing.
You want to execute code in the top-level urls.py
. That module is imported and executed once.
urls.py
from django.confs.urls.defaults import *
from my_app import one_time_startup
urlpatterns = ...
one_time_startup()
This question is well-answered in the blog post Entry point hook for Django projects, which will work for Django >= 1.4.
Basically, you can use <project>/wsgi.py
to do that, and it will be run only once, when the server starts, but not when you run commands or import a particular module.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
# Run startup code!
....
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
As suggested by @Pykler, in Django 1.7+ you should use the hook explained in his answer, but if you want that your function is called only when run server is called (and not when making migrations, migrate, shell, etc. are called), and you want to avoid AppRegistryNotReady exceptions you have to do as follows:
file: myapp/apps.py
import sys
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'my_app'
def ready(self):
if 'runserver' not in sys.argv:
return True
# you must import your modules here
# to avoid AppRegistryNotReady exception
from .models import MyModel
# startup code here