Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 *
like image 942
Trọng Nhân Avatar asked Apr 26 '26 13:04

Trọng Nhân


1 Answers

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.

like image 67
AzyCrw4282 Avatar answered Apr 28 '26 04:04

AzyCrw4282