Detect if a python module changes and then reload

Question:

So I’m developing a rather large python project with a number of modules. The “main” (runnable) module is a daemon (a Thrift daemon, actually) which calls off to other modules for its actual functionality. Starting up the daemon takes a long time because some of the modules have a rather lengthy and involved initialization processes.

So when I start the daemon, I wait… let’s say… 2 minutes for everything to load, which isn’t too bad in the grand scheme of things. However for development it becomes a major pain because I need to restart the daemon EVERY TIME which has been wasting a lot of my time.

Most modules only takes a few seconds to load. Ideally what I’d like to do is detect when any of the files in a particular module have changed, and reload that particular module. I’ve already figured out how to reload a module, but at this point I can’t figure out how to watch a particular module for changes. Keep in mind that a module isn’t a single .py file in this case, but rather a directory with __init__.py and 5-10 .py files, so I need to detect when any of them have changed.

Here is the project layout (if it makes any difference at all)

project
| -- daemonize.py
| -- main.py
| -- moduleA
|    | -- __init__.py
|    | -- happy_panda.py
|    ` -- sad_panda.py
| -- moduleB
|    | -- __init__.py
|    | -- takes_forever_to_load.py
|    ` -- seriously_get_some_coffee.py
| -- moduleC
|    | -- __init__.py
|    | -- frequently_changes.py
|    | -- reasons_i_hate_my_job.txt
|    ` -- home_address_of_moduleB_developer.txt
` -- service.py <-- uses modules A, B, and C

Any ideas or suggestions are appreciated.

EDIT

Thanks for the great feedback. Here is the code I created based on the suggestions. There’s a small bug where pyinotify seems to be getting more than one notification, but it’s a very small problem for me so I’m not going to fix it.

https://gist.github.com/1013122

Asked By: Chris Eberle

||

Answers:

I would look at all of the files, and detect if a file was modified. If it was, I would reload it.

Answered By: Nick ODell

Detect File Change Without Polling

Coupled with you already knowing how to reload your module this answer pretty much fills it out. It uses Inotify to “notify” (see what they did there) the program when the file is modified.

Answered By: Paystey

It’s an old question, but if you stumble upon it, here is a solution if you use IPython:

Enter the following lines in the IPython prompt at any time

%load_ext autoreload
%autoreload 2

For more options, check out this link:
https://ipython.readthedocs.io/en/stable/

Answered By: PierreE
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.