python看门狗运行不止一次 [英] python watchdog runs more than once

查看:461
本文介绍了python看门狗运行不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习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_pathjob.done结尾.因此,


Above there were two events with src_path ending in job.done. Thus,

    if getext(event.src_path) == '.done':
        run_something()

运行两次,因为有一个FileCreatedEventFileModifiedEvent. 您最好只监视FileModifiedEvent s.

runs twice because there is a FileCreatedEvent and a FileModifiedEvent. You might be better off only monitoring FileModifiedEvents.

这篇关于python看门狗运行不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆