【问题标题】:python watchdog runs more than oncepython看门狗运行不止一次
【发布时间】:2014-12-24 18:03:06
【问题描述】:

我正在尝试学习 python-watchdog,但我有点困惑为什么我设置的作业运行不止一次。所以,这是我的设置:

#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 只是一个shell 脚本,当在/path/to/some/directory/bin 上找到扩展名为.done 的文件时,我想运行它

#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 运行了3 次而不是1 次......

我很困惑为什么它运行三次而不是一次(我确实删除了我的 bash 脚本中的 job.done 文件)

【问题讨论】:

  • ‘While 1’ 和 ‘While true’ 是糟糕的代码,我猜你的代码甚至没有必要,因为‘join’调用通常会阻塞,直到满足条件。您的代码将在某些时候无限期阻塞,创建一个浪费系统资源的僵尸进程
  • 实际上,我直接从教程中提取了代码:ginstrom.com/scribbles/2012/05/10/… 我现在已经从代码中删除了两个‘While 1‘ and ‘While true‘。再次感谢您的提示。

标签: python python-watchdog


【解决方案1】:

要调试看门狗脚本,打印看门狗所看到的事件很有用。一个文件编辑或 CLI 命令,例如touch,可能会导致多个看门狗事件。例如,如果您插入打印语句:

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 结尾。因此,

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

运行两次,因为有一个 FileCreatedEvent 和一个 FileModifiedEvent。 你最好只监控FileModifiedEvents。

【讨论】:

  • 这简直太棒了 - 非常感谢 - 我现在使用 on_created 来监视新创建的文件(因为无论如何我都删除了我的 bash 脚本上的 job.done)。按预期工作!接受了你的回答:)
  • 你认为我最好找FileCreatedEvent而不是FileModifiedEvent吗?
  • 好吧,在这种情况下,我认为这并不重要。我建议FileModifiedEvent 只是因为它发生在最后,所以您知道写入文件的任何内容都已写入。在这种情况下,听起来你没有在读取文件,所以没关系。
  • 哦,再次感谢 unutbu 的解释!祝你圣诞快乐!
  • 伙计们,我正在尝试创建一个简单的同步工具,它将这些 CRUD 事件发送到同步处理程序。但显然在任何 CRUD 操作上,除了更新之外,都会触发多个事件,我只想获取第一个最多的事件。例如,在 created 的情况下,created 事件,然后是创建文件所在目录的修改事件,然后是新文件创建事件的修改。如何获取创建的事件并使用它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多