python看门狗运行不止一次 [英] python watchdog runs more than once
问题描述
我正在尝试学习python-watchdog,但是我有点困惑为什么我设置的作业运行不止一次.所以,这是我的设置:
I am trying to learn python-watchdog, but I am sort of confused why the job I set up runs more than once. So, here is my set up:
#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
..其中./run.sh
只是我想在/path/to/some/directory/bin
..where ./run.sh
is just a shell script I would like to run when a file with an extension .done
is found on /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
运行了三次,而不是一次.
However, when I issue a python observer.py
and then do a touch job.done
on /path/to/some/directory/bin
, I see that my shell script ./run.sh
runs three times and not one..
我很困惑为什么它运行三次而不是一次(我确实删除了bash脚本中的job.done
文件)
I am confused why this runs thrice and not just once (I do delete the job.done
file on my bash script)
推荐答案
要调试监视程序脚本,打印监视程序看到的事件很有用.一个文件编辑或CLI命令(例如touch
)可能会导致多个看门狗事件.例如,如果您插入打印语句:
To debug watchdog scripts, it is useful to print what watchdog is seeing as events. One file edit or CLI command, such as touch
, can result in multiple watchdog events. For example, if you insert a print statement:
class ChangeHandler(FileSystemEventHandler):
def on_any_event(self, event):
print(event)
记录运行中的每个事件
% touch job.done
产生
2014-12-24 13:11:02 - <FileCreatedEvent: src_path='/home/unutbu/tmp/job.done'>
2014-12-24 13:11:02 - <DirModifiedEvent: src_path='/home/unutbu/tmp'>
2014-12-24 13:11:02 - <FileModifiedEvent: src_path='/home/unutbu/tmp/job.done'>
上面有两个事件,src_path
以job.done
结尾.因此,
Above there were two events with src_path
ending in job.done
. Thus,
if getext(event.src_path) == '.done':
run_something()
运行两次,因为有一个FileCreatedEvent
和FileModifiedEvent
.
您最好只监视FileModifiedEvent
s.
runs twice because there is a FileCreatedEvent
and a FileModifiedEvent
.
You might be better off only monitoring FileModifiedEvent
s.
这篇关于python看门狗运行不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!