Python看门狗重复事件 [英] Python watchdog duplicate events

查看:465
本文介绍了Python看门狗重复事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个修改后的看门狗示例,以便监视已添加到Windows中特定目录的.jpg照片的文件.

I've created a modified watchdog example in order to monitor a file for .jpg photos that have been added to the specific directory in Windows.

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

paths = []

xp_mode = 'off'

class FileHandler(FileSystemEventHandler):

    def on_created(self, event):
        if xp_mode == 'on':
            if not event.is_directory and not 'thumbnail' in event.src_path:
                print "Created: " + event.src_path
                paths.append(event.src_path)

    def on_modified(self, event):
        if not event.is_directory and not 'thumbnail' in event.src_path:
            print "Modified: " + event.src_path
            paths.append(event.src_path)

if __name__ == "__main__":
    path = 'C:\\'
    event_handler = FileHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observe r.stop()

    observer.join()

我注意到的一件事情是,添加文件时,会同时调用on_created和on_modified!为了解决这个问题,我决定只使用on_modified方法. 但是,我开始注意到这还会导致多个回调,但这一次是on_modified方法!

One of the things that I have noticed that when a file is added, both on_created and on_modified is called! To combat this problem, I decided to only use the on_modified method. However, I am starting to notice that this also causes multiple callbacks, but this time to the on_modified method!

Modified: C:\images\C121211-0008.jpg
Modified: C:\images\C121211-0009.jpg
Modified: C:\images\C121211-0009.jpg <--- What?
Modified: C:\images\C121211-0010.jpg
Modified: C:\images\C121211-0011.jpg
Modified: C:\images\C121211-0012.jpg
Modified: C:\images\C121211-0013.jpg

我无法弄清楚为什么会发生这种情况!这似乎也不是一致的.如果有人可以阐明这个问题,将不胜感激.

I cannot figure out for the life of me why this is happening! It doesn't seem to be consistent either. If anyone could shed some light on this issue, it will be greatly appreciated.

有类似的文章,但它适用于Linux:修改了Python watchdog并创建了重复的事件

There was a similar post, but it was for Linux: python watchdog modified and created duplicate events

推荐答案

当进程写入文件时,它首先创建该文件,然后一次写入一个内容.

When a process writes a file, it first creates it, then writes the contents a piece at a time.

您看到的是与这些操作对应的一组事件.有时,这些片段的编写速度足够快,Windows只能为所有这些片段发送一个事件,而其他时候您会收到多个事件.

What you're seeing is a set of events corresponding to those actions. Sometimes the pieces are written quickly enough that Windows only sends a single event for all of them, and other times you get multiple events.

这很正常...根据周围代码需要执行的操作,保留修改后的路径名的set而不是list可能是有意义的.

This is normal... depending on what the surrounding code needs to do, it might make sense to keep a set of modified pathnames rather than a list.

这篇关于Python看门狗重复事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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