best practices for location of start up code

61 views
Skip to first unread message

Larry Martell

unread,
Sep 22, 2017, 5:58:57 PM9/22/17
to django...@googlegroups.com
We have some code we want to run 1 time when our django app is
started. What is the best place for this? I tried putting it in my
app's config function, but from there I get:

*** AppRegistryNotReady: Apps aren't loaded yet.

Antonis Christofides

unread,
Sep 23, 2017, 11:18:13 AM9/23/17
to django...@googlegroups.com

Hi,

I believe that the standard place for this is AppConfig.ready().

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com

James Schneider

unread,
Sep 23, 2017, 1:35:32 PM9/23/17
to django...@googlegroups.com
Once as in only when the app is initially deployed, or once every time the web server process is started? 

Note that if you place code to run every time the web process is started, that it will run for every process, and most web servers spawn multiple processes.

-James

Larry Martell

unread,
Sep 23, 2017, 2:41:14 PM9/23/17
to django...@googlegroups.com
I'm looking to run it once when the web server is started (not each
time another process of the web server is started)

Larry Martell

unread,
Sep 23, 2017, 2:52:30 PM9/23/17
to django...@googlegroups.com
I am using nginx and uwsgi. I tried experimenting with Antonis
Christofides suggestion of using AppConfig.ready(). That seems to get
called just once when I start (or restart uwsgi). I tried many
connections to the server from different browsers and computers, but
still that was only called once. Is that something I can count on?

Note, when running the devel django server it was called twice at
start up, but when the 'real' server it was only called once. Anyone
know why I got the 2 calls here?

Antonis Christofides

unread,
Sep 23, 2017, 6:22:28 PM9/23/17
to django...@googlegroups.com
Hello Larry,

The Django development server runs in more than one thread, which is probably
why your initialization code run twice.

OTOH, it run only once on production probably because you have configured uwsgi
to run only one process (it's possible that this is the default on single-core
machines). However, we usually want to run at least two processes.

Running initialization code only once per wsgi server (re)start instead of once
per process start seems a bit strange. Why do you need this?

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com

Larry Martell

unread,
Sep 25, 2017, 9:56:37 AM9/25/17
to django...@googlegroups.com
On Sat, Sep 23, 2017 at 6:21 PM, Antonis Christofides
<ant...@djangodeployment.com> wrote:
> Hello Larry,
>
> The Django development server runs in more than one thread, which is probably
> why your initialization code run twice.
>
> OTOH, it run only once on production probably because you have configured uwsgi
> to run only one process (it's possible that this is the default on single-core
> machines). However, we usually want to run at least two processes.

I have 5 uwsgi processes - I was only seeing the init code run once
because I was just sending uwsgi a reload signal. If I killed it and
restarted it I was seeing the init code run 5 times.

> Running initialization code only once per wsgi server (re)start instead of once
> per process start seems a bit strange. Why do you need this?

I had not been given all the requirements. Turns out what we need to
do should not be part of the server init, so we moved it somewhere
else.
Reply all
Reply to author
Forward
0 new messages