Filemon

The file-monitor example demonstrates using RPyC-based events to be notified of changes to a remote file system. The service starts a remote thread that monitors a file (using os.stat), and reports changes via an asynchronous client callback.

Service

class FileMonitorService(rpyc.Service):
    class exposed_FileMonitor(object):
        def __init__(self, filename, callback, interval = 1):
            self.filename = filename
            self.interval = interval
            self.callback = rpyc.async(callback)   # <<== make the callback async
            ...
            self.thread = Thread(target = self.work)
            self.thread.start()
        def exposed_stop(self):
            ...
        def work(self):
            while self.active:
                stat = os.stat(self.filename)
                if self.last_stat is not None and self.last_stat != stat:
                    self.callback(self.last_stat, stat)
                self.last_stat = stat
                time.sleep(self.interval)

Client

conn = rpyc.connect_by_service("FileMonitor", host = "somehost")
 
# create a bg thread to process incoming "events", so we can mind our own business, 
# while the rpyc connection will be served in the background
bgsrv = rpyc.BgServingThread(conn)
 
# this is the callback that gets the old and new stats
def on_file_changed(oldstat, newstat):
    print "file changed"
    print "    old stat: %s" % (oldstat,)
    print "    new stat: %s" % (newstat,)
 
# create a filemon
mon = conn.root.FileMonitor("/some/path/to/file.txt", on_file_changed) 
 
# now when the file changes, our callback will be invoked
page_revision: 7, last_edited: 1227297211|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License