Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 10, 2014

Conversation

Falldog
Copy link
Contributor

@Falldog Falldog commented Sep 5, 2014

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.

None yet

2 participants