by BiteCode_dev · 6y ago · 0 comments
If you want autoreload for dev (e.g: like django dev server), better restart the entire process.
There is a 7 years old project that can help you with that called watchdog:
https://pythonhosted.org/watchdog/
It's a library that uses the fastest way available (e.g: inotify,kqueue...) to react to file changes (you can filter by types such as create, delete, write, etc) and triggers whatever code you want, passing you the metadata.If you just want something very simple, you don't have to code, you can install watchdog with its optional watchmedo dependancy:
pip install watchdog[watchmedo]
It will give you the watchmedo command, that lets you do something like this: watchmedo shell-command --patterns="*.py" --recursive \
--command='python your_script_to_rerun.py' .It is built specifically to restart things like servers as it was implemented for the Pylons Project (Pyramid web framework).
However it does not work for just watching a directory + running a command, it is specifically for watching Python code.
On the other hand, it's probably more performant than watchdog if you have a lot of files and need to fallback on manual watching.
Plus, if you are not a Python person, you may not want to use pip.