Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules for automatic Django reloading when one of the code files is changed?

Tags:

python

django

I noticed that ./manage.py runserver automatically reloads when my views.py file is changed. How doesthe underlying code that drives it work?

like image 250
d33tah Avatar asked Jul 27 '15 17:07

d33tah


1 Answers

Automatic Django Server Restart:

Django tries to poll file modification timestamps each second. If it sees there are any changes. it restarts the server.

So basically, Django server checks every second the modification timestamps of every file. If it sees a change in any of them, it will trigger a server restart.

However, adding a new file does not trigger a restart so you will have to restart the server yourself in that scenario.

Exception: If you are using Linux and install pyinotify, kernel signals will be used to autoreload the server.

As per django docs,

If you are using Linux and install pyinotify, kernel signals will be used to autoreload the server (rather than polling file modification timestamps each second). This offers better scaling to large projects, reduction in response time to code modification, more robust change detection, and battery usage reduction.

System Checks performed while restarting the server:

System check framework is used to perform checks on Django projects.

The system check framework is a set of static checks for validating Django projects. It detects common problems and provides hints for how to fix them.

When you start the server, and each time you change Python code while the server is running, the system check framework checks our entire Django project for some common errors. If any errors are found, they are printed to standard output.

like image 160
Rahul Gupta Avatar answered Oct 18 '22 20:10

Rahul Gupta