Skip to content

Instantly share code, notes, and snippets.

@non117
Created December 18, 2013 03:12
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save non117/8016703 to your computer and use it in GitHub Desktop.
python inotify daemon
import multiprocessing
import os
import time
from functools import partial
import pyinotify
def is_alive(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
class Handler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print event.pathname
def worker(directory, parent_pid):
wm = pyinotify.WatchManager()
notifier = pyinotify.ThreadedNotifier(wm, Handler())
notifier.start()
wdd = wm.add_watch(directory, pyinotify.IN_CREATE)
waiting = True
while waiting:
if is_alive(parent_pid):
pass
else:
waiting = False
notifier.stop()
def main()
directory = '/tmp'
f = partial(worker, directory, os.getpid())
p = multiprocessing.Process(target=f)
p.daemon = True
p.start()
time.sleep(10)
exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment