Why watchdog is delayed to show file changes?

113 views
Skip to first unread message

calfland79

unread,
Jul 30, 2015, 10:14:22 PM7/30/15
to watchdog-python
I am learning watchdog. My code is just to detect creation of *.rst files and print their path. After running this code, creating a new rst file in the same folder. It took about 10 mins to have reaction that path was printed. don't know why.

My test code is as follow:

import os
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

BASEDIR = os.path.abspath(os.path.dirname(__file__))

def run_tests(filepath):
    "Run unit tests with unittest."
    print filepath
     

def getext(filename):
    "Get the file extension."

    return os.path.splitext(filename)[-1].lower()

class ChangeHandler(FileSystemEventHandler):
    """
    React to changes in Python and Rest files by
    running unit tests (Python) or building docs (.rst)
    """

    def on_any_event(self, event):
        "If any file or folder is changed"

        if event.is_directory:
            return
        elif getext(event.src_path) == '.rst':
            run_tests(event.src_path)

def main():
    """
    Called when run as main.
    Look for changes to code and doc files.
    """

    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()
 
Reply all
Reply to author
Forward
0 new messages