Handling OSError from Watchdog

蹲街弑〆低调 提交于 2019-12-11 02:05:54

问题


I have been using tkinter combined watchdog module to handle some uploading requests. Most of the time it works fine, but sometimes our network drive goes unstable and disconnects for certain amount of time. However i am unable to find the correct place to catch this error.

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import tkinter as tk

root = tk.Tk()
path = [r"Network path 1",r"Network path 2"]

class MyGui:
    def __init__(self,master):
        self.master = master
        self.but = tk.Button(master,text="Click to start observer",command=self.start_observer)
        self.but.pack()

    def start_observer(self):
        for i in path:
            observer.schedule(event_handler, path=i, recursive=False)
        observer.start()
        self.but.config(state="disabled",text="observer started")
        print ("Observer started")

class MyHandler(FileSystemEventHandler):
    def on_created(self, event):
        current_file = event.src_path
        print (current_file)

event_handler = MyHandler()
observer = Observer()

gui = MyGui(root)
root.mainloop()

This is what i got after letting it run for a couple days:

Exception in thread Thread-4:
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\api.py", line 146, in run
    self.queue_events(self.timeout)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\read_directory_changes.py", line 75, in queue_events
    winapi_events = read_events(self._handle, self.watch.is_recursive)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\winapi.py", line 346, in read_events
    buf, nbytes = read_directory_changes(handle, recursive)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\winapi.py", line 306, in read_directory_changes
    raise e
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\winapi.py", line 302, in read_directory_changes
    ctypes.byref(nbytes), None, None)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\winapi.py", line 107, in _errcheck_bool
    raise ctypes.WinError()
OSError: [WinError 64] The specified network name is no longer available.

Can anyone suggest how to handle this exception correctly?


回答1:


This is how i solved this problem:

    from watchdog import observers
    from watchdog.observers.api import DEFAULT_OBSERVER_TIMEOUT, BaseObserver


    class MyEmitter(observers.read_directory_changes.WindowsApiEmitter):
        def queue_events(self, timeout):
            try:
                super().queue_events(timeout)
            except OSError as e:
                print(e)
                connected = False
                while not connected:
                    try:
                        self.on_thread_start()  # need to re-set the directory handle.
                        connected = True
                        print('reconnected')
                    except OSError:
                        print('attempting to reconnect...')
                        time.sleep(10)


    observer = BaseObserver(emitter_class=MyEmitter, timeout=DEFAULT_OBSERVER_TIMEOUT)
    ...

Subclassing WindowsApiEmitter to catch the exception in queue_events. In order to continue after reconnecting, watchdog needs to re-set the directory handle, which we can do with self.on_thread_start().

Then use MyEmitter with BaseObserver, we can now handle losing and regaining connection to shared drives.



来源:https://stackoverflow.com/questions/54397283/handling-oserror-from-watchdog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!