Skip to content

Instantly share code, notes, and snippets.

@mikemilano
Last active September 18, 2016 03:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikemilano/6923819 to your computer and use it in GitHub Desktop.
Save mikemilano/6923819 to your computer and use it in GitHub Desktop.
Python's Twisted framework watching a directory and broadcasting changes to zeromq.
from twisted.internet import inotify
from twisted.python import filepath
import zmq
from pprint import pprint
class FileSystemWatcher(object):
def __init__(self, path_to_watch):
self.path = path_to_watch
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUB)
self.socket.bind("tcp://127.0.0.1:5000")
def Start(self):
notifier = inotify.INotify()
notifier.startReading()
notifier.watch(filepath.FilePath(self.path),
callbacks=[self.OnChange])
def OnChange(self, watch, path, mask):
msg = "event %s %s" % (', '.join(inotify.humanReadableMask(mask)), path.dirname() + '/' + path.basename())
#msg = "test ", path
self.socket.send(msg)
if __name__ == '__main__':
from twisted.internet import reactor
fs = FileSystemWatcher('/home/vagrant/test')
fs.Start()
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment