Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python watchdog modified and created duplicate events

Running on Ubuntu, every time i create a file i get a modified and a created event.

Is this by design or am i doing something wrong?

I'm using the event handler class PatternMatchingEventHandler

event_handler = MediaFileHandler(ignore_directories=True) 
observer = Observer() 
observer.schedule(event_handler, path=directory, recursive=True) 
observer.start()

If this is the correct behavior, can i safely ignore the created event?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer: f = open(... , 'w') generates a FileCreatedEvent, f.flush() or f.close() can generate a FileModifiedEvent. So yes, creating a file often generates both FileCreatedEvent and FileModifiedEvents.

Whether or not you can safely ignore FileCreatedEvents depends on what you are trying to do. If you are interested in reacting whenever a file is created, then you need to handle FileCreatedEvents, and perhaps ignore FileModifiedEvents, since it is possible to generate FileModifiedEvents when modifying a file without generating FileCreatedEvents.

Play around with the canonical watchdog script (below) and all should be clearer.


Long answer: To see what is happening, run the canonical watchdog program straight from the docs:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

And from a terminal:

% mkdir ~/tmp
% cd ~/tmp
% script.py 

Now in a Python interpreter when you open a file in w mode:

In [126]: f = open('/home/unutbu/tmp/foobar', 'w')

The terminal prints

2014-02-05 16:29:34 - <FileCreatedEvent: src_path=/home/unutbu/tmp/foobar>

When you write to the file watchdog does not report any event:

In [127]: f.write('Hi')

But when you flush,

In [128]: f.flush()

it reports a FileModifiedEvent:

2014-02-05 16:29:55 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>

If you write more stuff to the file:

In [129]: f.write(' there')

similarly, a FileModifiedEvent is reported when you close the file, since more output is flushed to disk:

In [130]: f.close()

2014-02-05 16:30:12 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...