DEV Community

Cover image for Replacing django's default Statreloader with Facebook's Watchman
salem ododa
salem ododa

Posted on

Replacing django's default Statreloader with Facebook's Watchman

Ever thought what went on behind the scene when you run
python manage.py runserver
in your project's base directory during development?
Normally you get a familiar output:

$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
March 24, 2022 - 11:54:06
Django version 3.2.11, using settings 'wallet.settings'
Starting development server at http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

As indicated at the top of the terminal output, you see
Watching for file changes with StatReloader
Statreloader which is the default "file-watcher/file-reloader" for auto-reloading the web server whenever there's a change in .py files during development is no more than a simple class which works by running a loop that checks for file changes every 1 second, this can be quite in-efficient when working on larger projects.
Now except you're experimenting other alternatives to the default Statreloader or working on a quite large codebase where efficiency and resource optimization(like battery usage, memory) is a big deal, i'd advice you to stick with statreloader as it doesn't require any pre-setup to work with.

Introducing Watchman

Watchman is a file watching service introduced by Facebook for watching file changes, it relies on the operating system to send signals whenever a change is made to a file.
Django get notified when there's been a recent change to a file and quickly reload's the server to include the recently made changes.
This makes watchman a better alternative as it doesn't keep any process running when there isn't a file change, which leads to better CPU resource optimization.

Installing Watchman

Watchman relies on the operating system it's running on to get file change notification signals, so it's most likely user experience would defer based on your choice of OS.

Personally i use a linux/debian distro and i found this amazing article on setting up Watchman on your linux pc:
Setting up watchman on linux
If you're running a windows/Mac Os, you can setup Watchman via the instructions specified on their official docs page: Setup Watchman on windows or Macos
it's pretty straightforward setting it up with chocolatey on windows.

Setting up Django to use WatchmanReloader

Getting django to work with watchman is very simple, first you have to cd into your base directory and install watchman client/wrapper for python i.e pywatchman.

activate your virtual environment
$ source walletenv/bin/activate

install pywatchman
$ pip install pywatchman

rerun your development server
$ python3 manage.py runserver

you should see that django is now using WatchmanReloader

Watching for file changes with WatchmanReloader
Performing system checks...

System check identified no issues (0 silenced).
March 25, 2022 - 13:53:33
Django version 3.2.11, using settings 'wallet.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Enter fullscreen mode Exit fullscreen mode

If you're building an application that has setups that aren't particularly related to what you're currently working on, you can configure watchman to ignore notifications from these particular dirs/files by simply including a .watchmanconfig file in the same directory where your manage.py file is.
touch .watchmanconfig
nano .watchmanconfig
{
"ignore_dirs": ["DIRECTORY_YOU'd_WANT_TO_IGNORE_CHANGES_FROM"]
}

Conclusion

Performance is a vital yardstick when building application that would scale, optimizing development process is also a very significant factor to this.
In this short article we've seen how to optimize our development process by using a more faster and memory friendly file watching service.
I hope you enjoyed the article and in the meantime, let me know what you think about the article and possibly any amendments that could be made to improve it's user experience, thanks for reading and have a great time!

Oldest comments (0)