Yueyoum
Yueyoum

Reputation: 3073

django + uwsgi, where to place my startup code?

I try to place my startup code in mysite/__init__.py

When I run python manage.py run, the startup code will be executed.

But when I run django with uwsgi, this startup code not executed.

I have tried to put my startup code in several different places, but it gets executed for none of them when uwsgi is started.

What should I do to have the startup code executed when uwsgi starts?

Upvotes: 1

Views: 970

Answers (1)

freakish
freakish

Reputation: 56467

Put your startup code in a separate file, for example startup.py and then alter these two files:

manage.py

# some code
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[project name].settings")
if __name__ == "__main__":
    import startup
    # the rest of the code

[project name]/wsgi.py

# some code
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[project name].settings")
import startup
# the rest of the code

Note the order: import is always after environ setting (might not be important, depends on what startup does).

In Django1.7 you can use ready function per application. Read this:

https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready

Upvotes: 2

Related Questions