Skip to content

fix the issue: watch & unwatch the same folder on Windows #270

Merged
tamland merged 1 commit into
gorakhargosh:masterfrom
Falldog:windows_shutdown_issue
Sep 10, 2014
Merged

fix the issue: watch & unwatch the same folder on Windows #270
tamland merged 1 commit into
gorakhargosh:masterfrom
Falldog:windows_shutdown_issue

Conversation

@Falldog

@Falldog Falldog commented Sep 5, 2014

Copy link
Copy Markdown
Contributor

fix the issue : watch & unwatch the same folder on Windows may cause exception "Access Denied"
should close the dir handle

testing code

import os
import sys
import time
import shutil
import logging
import tempfile
import threading
from os.path import join, dirname, abspath
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

class ObserverThread(threading.Thread):
    def __init__(self, folder):
        threading.Thread.__init__(self)
        self.folder = folder
        self.stop_evt = threading.Event()
    def stop(self):
        self.stop_evt.set()
    def run(self):
        event_handler = LoggingEventHandler()
        observer = Observer()
        observer.schedule(event_handler, self.folder, recursive=True)
        observer.start()
        self.stop_evt.wait()
        observer.stop()
        observer.join()

def main():
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    folder = tempfile.mkdtemp()    # watch & unwatch the same folder 10 times
    for i in range(10):
        if not os.path.exists(folder):
            os.mkdir(folder)
        tmp_file = join(folder, 'tmp.txt')
        print 'process %d temp_file=%s' % (i, tmp_file)

        t = ObserverThread(folder)
        t.start()
        with open(tmp_file, 'w') as f:
            f.write('abc')
        os.remove(tmp_file)
        t.stop()
        t.join()

        shutil.rmtree(folder)

if __name__ == "__main__":
    main()

…exception "Access Denied"

should close the dir handle
tamland added a commit that referenced this pull request Sep 10, 2014
fix the issue: watch & unwatch the same folder on Windows
@tamland
tamland merged commit f15f381 into gorakhargosh:master Sep 10, 2014
CCP-Aporia pushed a commit to CCP-Aporia/watchdog that referenced this pull request Aug 13, 2020
fix the issue: watch & unwatch the same folder on Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants