Last active
September 18, 2016 03:43
-
-
Save mikemilano/6923819 to your computer and use it in GitHub Desktop.
Python's Twisted framework watching a directory and broadcasting changes to zeromq.
This file contains hidden or 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
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