1

My program utilises Qt's QFileSystemWatcher function to monitor a network directory (not on the local machine itself) for changes, and then run a script when a change is found. This function performs as required for the most part. The program is designed to run 24/7, which has raised some issues using this particular function.

The error which is causing issues is as follows:

QFileSystemWatcher: FindNextChangeNotification failed!! (The specified network name is no longer available.)

The functionality I'd like to implement is as follows:

  1. Build in error handling surrounding network availability for QFileSystemWatcher
  2. If the network becomes unavailable and the error is raised, go to Script()
  3. Run Script() for handling the unavailable network

Given that the QFileSystemWatcher function is established in the initialisation of the program, I'm not sure how to go about error handling. Here's the basic outline of my current code:

class Main(QMain, Ui_Main):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)

        self.DirectoryWatcher = QtCore.QFileSystemWatcher([r'U:\NetworkAddress\Directory'])
        self.DirectoryWatcher.directoryChanged.connect(self.GoToThisDirectory)

    def GoToThisDirectory(self):
        print("foo")

Is there a way to explicitly establish error handling for the 'FindNextChangeNotification' error? Any input would be greatly appreciated!

4

1 回答 1

1

根据上面 ekhumoro 的评论,我已经设法使用 qInstallMsgHandler 和 sys.excepthook 函数解决了这个问题。

import sys
import os
from PySide.QtCore import qInstallMsgHandler

def myCustomHandler(ErrorType, ErrorContext):
    print("Qt error found.")
    print("Error Type: " + str(ErrorType))
    print("Error Context: " + str(ErrorContext))

    #Error logging code
    #Error emailing code

    os.execv(sys.executable, [sys.executable] + sys.argv)

qInstallMsgHandler(myCustomHandler)

def ErrorHandling(ErrorType, ErrorValue, TraceBack):
    print("System error found.")
    print("Error Type: " + str(ErrorType))
    print("Error Value: " + str(ErrorValue))
    print("Traceback: " + str(TraceBack))

    #Error logging code
    #Error emailing code

    os.execv(sys.executable, [sys.executable] + sys.argv)

sys.excepthook = ErrorHandling

#Rest of the script

我的解决方案分别解决了与 Qt 和 Python/系统相关的错误,但以相同的方式处理它们。错误记录在 .log 文件中,并通过电子邮件发送给系统管理员,然后软件重新启动。感谢您引导我朝着正确的方向 ekhumoro!

于 2015-05-20T03:29:33.253 回答