我是 Python 新手,我正在尝试与我的家庭服务器一致地自动同步。我发现了看门狗并且正在尝试使用它,但是每当我运行“touch test.txt”时,该脚本就会不断启动新的一致进程。由于观察者使用从队列继承的类,因此我认为它应该在将第一个事件从队列顶部弹出后阻塞。我在这里忽略了什么吗?
代码:
#!/usr/bin/python
import sys
import subprocess
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ChangeHandler(FileSystemEventHandler):
def on_any_event(self, event):
subprocess.call(["/usr/bin/unison", "-batch", "-silent", "-ui", "text",
"default"])
if __name__ == "__main__":
observer = Observer()
observer.schedule(ChangeHandler(), '/home/philip', True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
输出:
props <-?-> props / props <-?-> props Documents props <-?-> props Downloads props <-?-> props / props <-?-> props Documents props <-?-> props Downloads props <-?-> props / props <-?-> props Documents props <-?-> props Downloads props <-?-> props / props <-?-> props Documents props <-?-> props Downloads props <-?-> props / props <-?-> props Documents props <-?-> props Downloads props <-?-> props / props <-?-> props Documents props <-?-> props Downloads
最佳答案
OP 在评论中充分回答了这个问题:
It took working with pyinotify to finally understand that I'm synchronizing a directory while simultaneously watching that same directory for filesystem events. Of course, i'll get multiple calls unless I suspend the Observer/Notifier when processing an event.
关于Python看门狗: Why is my subprocess called continuously?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250779/