Search code examples
pythondjangodjango-settingsdjango-3.2

Settings in Django run repeat when runserver


I don't know why my django app run settings/base.py 2 times. I think it would make my app slow down
In my settings/base.py I printed

print('this is base_dir')
print(BASE_DIR)

output is:

this is base_dir
F:\7.Django\BLOG_PROJECT\src_blog
this is base_dir
F:\7.Django\BLOG_PROJECT\src_blog

This is my settings file:

    ├── settings 
    |     ├──__init__.py 
    |     ├──base.py 
    |     ├──dev.py 
    |     ├──prod.py

and my settings\__init__.py file contain:

import os
from dotenv import load_dotenv
load_dotenv()

if os.environ['ENV_SETTING'] =='prod':
   from .prod import *
else:
   from .dev import *

from .base import *

Solution

  • This is probably related to the good old double thread spawning in Django. The way Django is setup it spawns two threads at the start, so one process is there to process requests and the other to watch if you changed any code so it can respawn the first one.

    If you print the following in settings.py

    import os
    print(os.getpid())
    

    You would see that it prints 2 different values. This is a standard django behaviour as far as I know.