环境:
python2.7
rpyc 3.4.2
官网demo,文件监控
#!/usr/bin/env python # coding:utf-8 # server.py import rpyc import os import time from threading import Thread class FileMonitorService(rpyc.SlaveService): class exposed_FileMonitor(object): def __init__(self, filename, callback, interval=1): self.filename = filename self.interval = interval self.last_stat = None self.callback = rpyc.async(callback) self.active = True self.thread = Thread(target=self.work) self.thread.start() def exposed_stop(self): self.active = False self.thread.join() 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) if __name__ == "__main__": from rpyc.utils.server import ThreadedServer ThreadedServer(FileMonitorService, port=18871).start()
#!/usr/bin/env python # coding: utf-8 #client.py import rpyc f = open("/tmp/floop.bloop", 'w') conn = rpyc.connect("localhost", 18871, service=rpyc.SlaveService) bg = rpyc.BgServingThread(conn) def on_file_changed(oldstat, newstat): print("file changed") print(" old stat: %s" %(oldstat,)) print(" new stat: %s" %(newstat,)) mon = conn.root.FileMonitor("/tmp/floop.bloop", on_file_changed) f.write("shmoop") f.flush() #wait for server callback, flask tornado django ---run_forever import time time.sleep(5)