我想学习python-watchdog,但我有点困惑为什么我设置的工作运行不止一次。所以,这里是我的设立:python看门狗运行不止一次
#handler.py
import os
from watchdog.events import FileSystemEventHandler
from actions import run_something
def getext(filename):
return os.path.splitext(filename)[-1].lower()
class ChangeHandler(FileSystemEventHandler):
def on_any_event(self, event):
if event.is_directory:
return
if getext(event.src_path) == '.done':
run_something()
else:
print "event not directory.. exiting..."
pass
观察者设置像这样:
#observer.py
import os
import time
from watchdog.observers import Observer
from handler import ChangeHandler
BASEDIR = "/path/to/some/directory/bin"
def main():
while 1:
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, BASEDIR, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()
最后,像这样的动作:
#actions.py
import os
import subprocess
def run_something():
output = subprocess.check_output(['./run.sh'])
print output
return None
..where ./run.sh
是只是一个shell脚本,我想运行时发现一个扩展名为.done
的文件/path/to/some/directory/bin
#run.sh
#!/bin/bash
echo "Job Start: $(date)"
rm -rf /path/to/some/directory/bin/job.done # remove the .done file
echo "Job Done: $(date)"
然而,当我发出python observer.py
,然后做一个/path/to/some/directory/bin
touch job.done
,我看到我的shell脚本./run.sh
运行三次,而不是一个..
我很困惑,为什么这个运行三次,不只是一次(我删除了我的bash脚本中的job.done
文件)
'虽然1'和'虽然是真的'是不好的代码,并且即时猜测你的代码,它甚至没有必要,因为'连接'调用通常阻塞直到条件满足。你的代码会在某个时候无限期地被阻塞,从而创建一个浪费系统资源的僵尸进程 – specializt
实际上,我直接从教程中取出代码:http://ginstrom.com/scribbles/2012/05/10/continuous-integration- in-python-using-watchdog /我现在已经从代码中删除了'while 1'和'While true'。再次感谢提示。 – JohnJ