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.
我的程序利用Qt的QFileSystemWatcher函数来监视网络目录(不在本地机器本身上)进行更改,然后在找到更改时运行脚本。此功能大部分都按要求执行。该程序旨在全天候运行,这引发了一些使用此特定功能的问题。
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:
我想实现的功能如下:
- Build in error handling surrounding network availability for
QFileSystemWatcher
- If the network becomes unavailable and the error is raised, go to
Script()
- Run
Script()
for handling the unavailable network
围绕QFileSystemWatcher的网络可用性构建错误处理
如果网络不可用并且出现错误,请转到脚本()
运行Script()以处理不可用的网络
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:
鉴于QFileSystemWatcher函数是在程序初始化时建立的,我不知道如何进行错误处理。这是我当前代码的基本概要:
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!
有没有办法明确建立'FindNextChangeNotification'错误的错误处理?任何投入将不胜感激!
1 个解决方案
#1
As per ekhumoro's comments above, I've managed to solve this question using both the qInstallMsgHandler and sys.excepthook functions.
根据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
My solution addresses Qt and Python/system-related errors separately, but handles them in the same way. The error is logged in a .log file, emailed to the system administrator and the software is restarted. Thanks for guiding me in the right direction ekhumoro!
我的解决方案分别解决了Qt和Python /系统相关的错误,但是以相同的方式处理它们。该错误记录在.log文件中,通过电子邮件发送给系统管理员,然后重新启动软件。感谢指导我朝正确的方向ekhumoro!
#1
As per ekhumoro's comments above, I've managed to solve this question using both the qInstallMsgHandler and sys.excepthook functions.
根据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
My solution addresses Qt and Python/system-related errors separately, but handles them in the same way. The error is logged in a .log file, emailed to the system administrator and the software is restarted. Thanks for guiding me in the right direction ekhumoro!
我的解决方案分别解决了Qt和Python /系统相关的错误,但是以相同的方式处理它们。该错误记录在.log文件中,通过电子邮件发送给系统管理员,然后重新启动软件。感谢指导我朝正确的方向ekhumoro!