Skip to content

Instantly share code, notes, and snippets.

@gotche
Last active October 29, 2015 18:47
Show Gist options
  • Save gotche/a3905ad36c4853ee6397 to your computer and use it in GitHub Desktop.
Save gotche/a3905ad36c4853ee6397 to your computer and use it in GitHub Desktop.
pyinotify example
"""
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