Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does django run everything twice? [duplicate]

Tags:

django

Whenever I put in a (random) print statement in somewhere like settings.py or any random .py file in django, it gets printed twice. Why?

Try this:

after setting the value on TEMPLATE_DIRS (in settings.py), just add a print statement after it: print TEMPLATE_DIRS

and you will get your templates directory printed out twice. Or if you're feeling old-fashioned, just add print "Hello World right after declaring TEMPLATE_DIRS, and it will print it twice.

Command to run : python manage.py runserver

like image 994
Games Brainiac Avatar asked May 14 '13 15:05

Games Brainiac


1 Answers

It's not running it twice, it's forking to 2 processes and each one of those is running it once.

Answered by: why is init module in django project loaded twice

It should be loaded only once... per process. I'm guessing that manage.py forks, and that two separate processes are launched.

and an article.

To verify this if you add this to your settings.py

import os
print(os.getpid())

It will print out 2 different process id numbers showing that it's spawned 2 processes.

You can read more about forking on wikipedia.

This also seems a duplicate of:

django - settings.py seems to load multiple times?

like image 134
Ewan Avatar answered Sep 28 '22 08:09

Ewan