-
-
Save bvcelari/d9b1c3d91f951f578570 to your computer and use it in GitHub Desktop.
pyinotify example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of use of inotify. Extracted from django.utils.autoreload | |
""" | |
import pyinotify | |
def inotify_logs_changed(): | |
class EventHandler(pyinotify.ProcessEvent): | |
def process_default(self, event): | |
print(event) | |
wm = pyinotify.WatchManager() | |
notifier = pyinotify.Notifier(wm, EventHandler()) | |
def update_watch(sender=None, **kwargs): | |
mask = ( | |
pyinotify.IN_MODIFY | | |
pyinotify.IN_CREATE | |
) | |
for path in gen_filenames(): | |
wm.add_watch(path, mask) | |
update_watch() | |
notifier.check_events(timeout=None) | |
notifier.read_events() | |
notifier.process_events() | |
notifier.stop() | |
# If we are here the code must have changed. | |
return EventHandler | |
def gen_filenames(): | |
return ['/tmp/cosita', ] | |
def main(): | |
while True: | |
inotify_logs_changed() | |
try: | |
main() | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment