python - 检查文件是否存在时防止“GUI 冻结”
问题描述
我正在尝试在 pyqt5 中构建一个文件侦听器应用程序。我的代码可以按我的意愿工作,但我想改进它。
有一个简单的按钮Listen
。当我单击它时,它会打开一个记事本并开始一直监听,直到a.txt
文件存在。存在后,新按钮Start
存在,旧按钮被删除。
我的问题是;a.txt
我的 GUI 在侦听文件时冻结,即使我使用threading
. 我用错了吗?你能修复我的代码吗?
我的主要代码;
from PyQt5 import QtCore, QtWidgets
import sys
import os
class ListenWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ListenWindow, self).__init__(parent)
self.setWindowTitle("Listen")
self.button_listen = QtWidgets.QPushButton('Listen', self)
font1 = self.button_listen.font()
font1.setPointSize(10)
self.button_listen.setFont(font1)
self.button_listen.setFixedSize(200, 50)
self.button_listen.clicked.connect(self.startToListen)
self.v_box1 = QtWidgets.QVBoxLayout(self)
self.v_box1.addWidget(self.button_listen)
self.h_box1 = QtWidgets.QHBoxLayout(self)
self.v_box1.addLayout(self.h_box1)
def abc(self):
while not os.path.exists('C:/Users/Wicaledon/PycharmProjects/myproject/a.txt'):
pass
if os.path.isfile('C:/Users/Wicaledon/PycharmProjects/myproject/a.txt'):
self.button_start = QtWidgets.QPushButton('Start', self)
font2 = self.button_start.font()
font2.setPointSize(10)
self.button_start.setFont(font2)
self.button_start.setFixedSize(200, 50)
self.h_box1.addWidget(self.button_start, 0, QtCore.Qt.AlignCenter)
else:
raise ValueError("%s isn't a file!" % 'C:/Users/Wicaledon/PycharmProjects/myproject/a.txt')
self.v_box1.removeWidget(self.button_listen)
def startToListen(self):
def thread_function(my_text):
import subprocess
import os
FNULL = open(os.devnull, 'w')
args = my_text
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=True)
# os.system(my_text)
return
import threading
my_text = "notepad"
x = threading.Thread(target=thread_function,args=(my_text,))
x.start()
y = threading.Thread(target=ListenWindow.abc(self))
y.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = ListenWindow()
window.setWindowTitle('Login')
window.show()
sys.exit(app.exec_())
我的创建代码a.txt
;
f=open("a.txt", "w+")
f.write("delete_me")
f.close()
解决方案
使用这种简单的 while 方法来检查文件是否存在并不是一件好事,因为它会不必要地消耗大量 CPU 资源。
此外,在处理 UI 时,使用任何类型的循环(while/for 或递归函数调用)都是一个坏主意:正如您已经看到的那样,它会阻塞界面;虽然 abhilb 提出的解决方案似乎可行,但它只是使 UI 响应,但无论如何都会使 CPU 保持峰值,即使在程序关闭且文件尚未创建之后也是如此。
PyQt 已经有一个文件监听器 QFileSystemWatcher,并且外部系统应该避免使用 Qt 已经提供的功能,这不仅适用于文件监听器,也适用于线程。
另一个要记住的重要方面是 Qt 有自己的事件循环,使用 python 的线程与之交互并不是一个好主意(事实上,每个UI 交互都必须在主线程中完成,即使使用Qt 的线程)。
如果你真的想做一个基本的文件监听器(例如,出于学习目的),你至少应该在循环中添加一个等待者,并且该循环必须在另一个线程中,否则 GUI 无论如何都会被阻塞。
以下是完全基于 Qt 的实现,基于您的代码。
import sys
from PyQt5 import QtCore, QtWidgets
fileToWatch = 'C:/Users/Wicaledon/PycharmProjects/myproject/a.txt'
editorProgram = 'notepad'
class ListenWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ListenWindow, self).__init__(parent)
self.setWindowTitle("Listen")
self.button_listen = QtWidgets.QPushButton('Listen', self)
font1 = self.button_listen.font()
font1.setPointSize(10)
self.button_listen.setFont(font1)
self.button_listen.setFixedSize(200, 50)
self.button_listen.clicked.connect(self.startToListen)
self.v_box1 = QtWidgets.QVBoxLayout(self)
self.v_box1.addWidget(self.button_listen)
# no parent with an already existing layout should be set for a new
# layout; in this case, the current widget already has the v_box1 layout
# set, therefore the new h_box1 layout should have no argument
self.h_box1 = QtWidgets.QHBoxLayout()
self.v_box1.addLayout(self.h_box1)
self.listener = QtCore.QFileSystemWatcher(self)
self.listener.directoryChanged.connect(self.checkFile)
def startToListen(self):
fileInfo = QtCore.QFileInfo(fileToWatch)
if fileInfo.exists():
self.createStart()
return
elif fileInfo.absolutePath() not in self.listener.directories():
self.listener.addPath(fileInfo.absolutePath())
# create an empty file so that there's no error when trying to open
# it in the editor
emptyFile = QtCore.QFile(fileToWatch)
emptyFile.open(emptyFile.WriteOnly)
emptyFile.close()
process = QtCore.QProcess(self)
process.start(editorProgram, [fileToWatch])
# optional: disable the interface until the program has quit
self.setEnabled(False)
process.finished.connect(lambda: self.setEnabled(True))
def checkFile(self, path):
fileInfo = QtCore.QFileInfo(fileToWatch)
if fileInfo.exists():
if self.h_box1:
# the layout already contains the start button, ignore
return
else:
self.createStart()
else:
# file has been [re]moved/renamed, maybe do something here...
pass
def createStart(self):
self.button_start = QtWidgets.QPushButton('Start', self)
font2 = self.button_start.font()
font2.setPointSize(10)
self.button_start.setFont(font2)
self.button_start.setFixedSize(200, 50)
self.h_box1.addWidget(self.button_start, 0, QtCore.Qt.AlignCenter)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = ListenWindow()
window.setWindowTitle('Login')
window.show()
sys.exit(app.exec_())
推荐阅读
- unity3d - 我想要一个UI按钮,点击它启动功能后,怎么可能?
- android - Android - 确定 LocalTime 是否在一组时间之间(包括午夜过后和午夜之后)
- node.js - 如何正确获取消息错误?
- python - 通过 PyQt5 Webview 中的功能更改 url
- python - 如何从嵌套列表中删除空行?
- mongodb - MongoDB UpdateOne 对冲突有自定义操作?
- joomla3.0 - 使用 Cloudbase 3 模板添加 Joomla 博客布局覆盖
- python - Faiss Kmeans图像聚类中的错误
- json - 从 serde_json 中的无类型 JSON 中提取数据时如何处理错误?
- flutter - 如何使用带有 getX boolean observable 的条件渲染