我想使用watchdog来阻止,直到文件在目录中发生变化。我正在做的是在变量为False
时休眠。但问题是,我不能打断睡眠;文件发生变化时,还有1秒的延迟时间。如何在睡眠后突破睡眠并继续睡觉?或者更一般地说,阻止直到文件发生变化?这是我的代码:
import sys
import time
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
if __name__ == "__main__":
observer = Observer()
def nothing():
pass
class FileChangeHandler(FileSystemEventHandler):
done = False
def on_any_event(self, event):
print 'Got event'
FileChangeHandler.done = True
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = FileChangeHandler()
observer.schedule(event_handler, path, recursive=True)
observer.start()
while not event_handler.done:
time.sleep(1)
print 'Done'
observer.stop()
observer.join()