- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我打算有一个 GUI,其中一个(后来的三个)线程以可调整的时间间隔(例如 10 秒)从不同的源读取实时数据,并在主窗口中绘制这些数据。
我正在使用 PyQt5 和 python 3.6。
读取是在工作线程中无限循环中执行的,如下所示:
class ReadingThread(QtCore.QObject):
output = QtCore.pyqtSignal(object)
def __init__(self, directory, interval):
QtCore.QObject.__init__(self)
self.directory=directory
self.stillrunning = True
self.refreshtime = interval
def run(self):
print('Entered run in worker thread')
self.stillrunning = True
while self.stillrunning:
outstring=self.read_last_from_logfile() # data reader function, not displayed
self.output.emit(outstring)
time.sleep(self.refreshtime)
@QtCore.pyqtSlot(int) # never called as loop is blocking?
def check_break(self, val):
if val:
self.stillrunning=False
else:
self.stillrunning = True
主线程如下所示,start() 和 stop() 通过按钮调用:
class Window(QtWidgets.QMainWindow, MainWindow.Ui_MainWindow):
def __init__(self, directory, interval):
super(Window, self).__init__()
self.thread = QtCore.QThread()
self.worker = ReadingThread(directory, interval)
emit_stop=QtCore.pyqtSignal(int)
def start(self):
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.output.connect(self.print_new_value)
self.emit_stop.connect(self.worker.check_break)
self.thread.start()
def stop(self):
self.emit_stop.emit(1)
# time.sleep(11)
if self.thread.isRunning(): #did not work either
self.thread.quit()
if self.thread.isRunning(): #did also not work
self.thread.terminate()
return
def print_new_value(self, value): #test function for output of values read by worker thread, working well
print (value)
return
def main():
app = QtWidgets.QApplication(sys.argv)
interval=10 #read every 10s new data
directory="/home/mdt-user/logfiles/T_P_logs"
gui = Window(directory,interval)
gui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我的问题是:如何让循环中的工作线程查找主线程发出的传入信号?或者换句话说:我怎样才能有一个像我的 self.stillrunning
这样的状态变量,它可以在工作线程外部设置/访问,但在工作线程内检查?
我想避免使用诸如 self.thread.terminate()
之类的方法来终止线程,但我尝试过但没有成功。
非常感谢您的帮助。我当然进行了搜索,但给出的答案和/或提出的问题对于我认为必须是一个简单的解决方案来说要么太长,要么不适用。
最佳答案
我不明白上述评论如何解决您的问题。根据设计,您的工作线程将无法接收信号,因为 while 循环会阻止工作线程事件循环,直到它中断并且方法完成。然后所有(在整个阻塞期间)接收到的信号都将起作用。好吧,从技术上讲,您收到的信号并没有被阻止,它们只是没有发挥作用,直到事件循环再次发挥作用......
我看到两种适合您的设计模式的解决方案(利用移动到线程)。
这里的想法是使用 QTimer。您向该计时器提供一个时间段(以毫秒为单位),每次经过该时间段时,该计时器将执行一项任务(即调用方法/函数)。由于您甚至可以传递 0 毫秒作为时间段,因此您可以模拟类似 while 循环的行为。好处是:事件循环不会被阻塞,并且每次超时后,接收到的信号都会起作用。
我修改了你的代码,通过 QTimer 实现了这个解决方案。我认为有了代码注释,这个例子就有点不言自明了。
class ReadingThread(QtCore.QObject):
output = QtCore.pyqtSignal(object)
def __init__(self, directory, interval):
#QtCore.QObject.__init__(self)
super(ReadingThread, self).__init__() # this way is more common to me
self.directory = directory
self.refreshtime = interval
# setting up a timer to substitute the need of a while loop for a
# repetitive task
self.poller = QTimer(self)
# this is the function the timer calls upon on every timeout
self.poller.timeout.connect(self._polling_routine)
def _polling_routine(self):
# this is what's inside of your while loop i.e. your repetitive task
outstring = self.read_last_from_logfile()
self.output.emit(outstring)
def polling_start(self):
# slot to call upon when timer should start the routine.
self.poller.start(self.refreshtime)
# the argument specifies the milliseconds the timer waits in between
# calls of the polling routine. If you want to emulate the polling
# routine in a while loop, you could pass 0 ms...
def polling_stop(self):
# This simply stops the timer. The timer is still "alive" after.
self.poller.stop()
# OR substitute polling_start and polling_stop by toggling like this:
def polling_toggle(self):
poller_active = self.poller.isActive()
if poller_active:
# stop polling
self.poller.stop()
else:
# start polling
self.poller.start(self.refreshtime)
class Window(QtWidgets.QMainWindow, MainWindow.Ui_MainWindow):
emit_start = QtCore.pyqtSignal()
emit_stop = QtCore.pyqtSignal()
def __init__(self, directory, interval):
super(Window, self).__init__()
self.init_worker()
def init_worker(self):
self.thread = QtCore.QThread()
self.worker = ReadingThread(directory, interval)
self.worker.moveToThread(self.thread)
self.worker.output.connect(self.print_new_value)
self.emit_start.connect(self.worker.polling_start)
self.emit_stop.connect(self.worker.polling_stop)
self.thread.start()
def start_polling(self):
self.emit_start.emit()
def stop_polling(self):
self.emit_stop.emit()
def finish_worker(self):
# for sake of completeness: call upon this method if you want the
# thread gone. E.g. before closing your application.
# You could emit a finished sig from your worker, that will run this.
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
def print_new_value(self, value):
print(value)
为了更好地了解如何使用 QThread 干净地完成此操作(这里的复杂性在于正确执行线程,QTimer 相对微不足道):https://realpython.com/python-pyqt-qthread/#using-qthread-to-prevent-freezing-guis
编辑:请务必查看 QTimer 的文档。您可以动态设置超时时间等等。
您可以例如将带有控制变量的字典传递到您的工作类/线程中,并使用它来打破循环。这是可行的,因为(下面是过于简化的语句)线程共享公共(public)内存,并且 python 中的可变对象共享内存中的同一对象(这已经在 SO 上进行了深入讨论)。我将在修改后的代码中对此进行说明,同时说明您会发现主线程和工作线程中的控制指令的内存 ID 相同:
class ReadingThread(QtCore.QObject):
output = QtCore.pyqtSignal(object)
def __init__(self, directory, interval, ctrl):
QtCore.QObject.__init__(self)
self.ctrl = ctrl # dict with your control var
self.directory = directory
self.refreshtime = interval
def run(self):
print('Entered run in worker thread')
print('id of ctrl in worker:', id(self.ctrl))
self.ctrl['break'] = False
while True:
outstring=self.read_last_from_logfile()
self.output.emit(outstring)
# checking our control variable
if self.ctrl['break']:
print('break because flag raised')
# might emit finished signal here for proper cleanup
break # or in this case: return
time.sleep(self.refreshtime)
class Window(QtWidgets.QMainWindow, MainWindow.Ui_MainWindow):
emit_stop=QtCore.pyqtSignal(int)
def __init__(self, directory, interval):
super(Window, self).__init__()
self.thread = QtCore.QThread()
self.ctrl = {'break': False} # dict with your control variable
print('id of ctrl in main:', id(self.ctrl))
# pass the dict with the control variable
self.worker = ReadingThread(directory, interval, self.ctrl)
def start(self):
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.output.connect(self.print_new_value)
self.thread.start()
def stop(self):
# we simply set the control variable (often refered to as raising a flag)
self.ctrl['break'] = True
这个解决方案几乎不需要改变你的代码,我肯定会认为它不像QT,甚至不干净,但它非常方便。有时您不想围绕您使用 GUI 工具包的事实来编写实验/长时间运行的任务。
这是我所知道的唯一方法,可以让你绕过阻塞的事件循环。如果有人对此有更清洁的解决方案,请让全世界知道。特别是因为这是以受控方式从多个点突破长时间运行的任务的唯一方法,因为您可以在整个重复例程中多次检查控制变量。
关于python - 在 PyQt5 中停止工作线程中的无限循环最简单的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68163578/
我在Windows 10中使用一些简单的Powershell代码遇到了这个奇怪的问题,我认为这可能是我做错了,但我不是Powershell的天才。 我有这个: $ix = [System.Net.Dn
var urlsearch = "http://192.168.10.113:8080/collective-intellegence/StoreClicks?userid=" + userId +
我有一个非常奇怪的问题,过去两天一直让我抓狂。 我有一个我试图控制的串行设备(LS 100 光度计)。使用设置了正确参数的终端(白蚁),我可以发送命令(“MES”),然后是定界符(CR LF),然后我
我目前正试图让无需注册的 COM 使用 Excel 作为客户端,使用 .NET dll 作为服务器。目前,我只是试图让概念验证工作,但遇到了麻烦。 显然,当我使用 Excel 时,我不能简单地使用与可
我开发了简单的 REST API - https://github.com/pavelpetrcz/MandaysFigu - 我的问题是在本地主机上,WildFly 16 服务器的应用程序运行正常。
我遇到了奇怪的情况 - 从 Django shell 创建一些 Mongoengine 对象是成功的,但是从 Django View 创建相同的对象看起来成功,但 MongoDB 中没有出现任何数据。
我是 flask 的新手,只编写了一个相当简单的网络应用程序——没有数据库,只是一个航类搜索 API 的前端。一切正常,但为了提高我的技能,我正在尝试使用应用程序工厂和蓝图重构我的代码。让它与 pus
我的谷歌分析 JavaScript 事件在开发者控制台中运行得很好。 但是当从外部 js 文件包含在页面上时,它们根本不起作用。由于某种原因。 例如; 下面的内容将在包含在控制台中时运行。但当包含在单
这是一本名为“Node.js 8 the Right Way”的书中的任务。你可以在下面看到它: 这是我的解决方案: 'use strict'; const zmq = require('zeromq
我正在阅读文本行,并创建其独特单词的列表(在将它们小写之后)。我可以使它与 flatMap 一起工作,但不能使它与 map 的“子”流一起工作。 flatMap 看起来更简洁和“更好”,但为什么 di
我正在编写一些 PowerShell 脚本来进行一些构建自动化。我发现 here echo $? 根据前面的语句返回真或假。我刚刚发现 echo 是 Write-Output 的别名。 写主机 $?
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我将一个工作 View Controller 类从另一个项目复制到一个新项目中。我无法在新项目中加载 View 。在旧项目中我使用了presentModalViewController。在新版本中,我
我对 javascript 很陌生,所以很难看出我哪里出错了。由于某种原因,我的功能无法正常工作。任何帮助,将不胜感激。我尝试在外部 js 文件、头部/主体中使用它们,但似乎没有任何效果。错误要么出在
我正在尝试学习Flutter中的复选框。 问题是,当我想在Scaffold(body :)中使用复选框时,它正在工作。但我想在不同的地方使用它,例如ListView中的项目。 return Cente
我们当前使用的是 sleuth 2.2.3.RELEASE,我们看不到在 http header 中传递的 userId 字段没有传播。下面是我们的代码。 BaggageField REQUEST_I
我有一个组合框,其中包含一个项目,比如“a”。我想调用该组合框的 Action 监听器,仅在手动选择项目“a”完成时才调用。我也尝试过 ItemStateChanged,但它的工作原理与 Action
你能看一下照片吗?现在,一步前我执行了 this.interrupt()。您可以看到 this.isInterrupted() 为 false。我仔细观察——“这个”没有改变。它具有相同的 ID (1
我们当前使用的是 sleuth 2.2.3.RELEASE,我们看不到在 http header 中传递的 userId 字段没有传播。下面是我们的代码。 BaggageField REQUEST_I
我正在尝试在我的网站上设置一个联系表单,当有人点击发送时,就会运行一个作业,并在该作业中向所有管理员用户发送通知。不过,我在失败的工作表中不断收到此错误: Illuminate\Database\El
我是一名优秀的程序员,十分优秀!