簡體   English   中英

從看門狗處理 OSError

[英]Handling OSError from Watchdog

我一直在使用 tkinter 組合看門狗模塊來處理一些上傳請求。 大多數情況下它工作正常,但有時我們的網絡驅動器會變得不穩定並在一定時間內斷開連接。 但是,我無法找到捕獲此錯誤的正確位置。

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()

這是我讓它運行幾天后得到的:

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.

誰能建議如何正確處理這個異常?

我是這樣解決這個問題的:

    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)
    ...

子類化WindowsApiEmitter以捕獲queue_events的異常。 為了在重新連接后繼續,看門狗需要重新設置目錄句柄,我們可以使用self.on_thread_start()

然后使用MyEmitterBaseObserver ,我們現在可以處理與共享驅動器的丟失和重新連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM