multithreading - What's the correct pattern for threading in a python qt5 application? -
i'm trying write pyqt5 application long running, not cpu intensive process. i'd able run without hanging ui, i'm trying use threading, since doesn't seem can run thread , have stop after gone through code can run again, i've tried setting thread wait variable change before running.
i know can't correct pattern running long processes in pyqt app.
import time import threading pyqt5 import qtwidgets, uic class myapp(qtwidgets.qmainwindow): _run_thread = false def __init__(self): qtwidgets.qmainwindow.__init__(self) self.ui = uic.loadui('myapp.ui', self) self.ui.start_thread_button.clicked.connect(self._run_thread_function) self._thread = threading.thread(target=self._run_thread_callback) self._thread.daemon = true self._thread.start() self.ui.show() def _run_thread_callback(self): while true: if self._run_thread: print("running thread code...") time.sleep(10) print("thread code finished") self._run_thread = false def _run_thread_function(self): print("starting thread...") self._run_thread = true def main(): app = qtwidgets.qapplication(sys.argv) myapp() sys.exit(app.exec_()) if __name__ == '__main__': main()
below simple demo showing how start , stop worker thread, , safely comminucate gui thread.
import sys pyqt5 import qtcore, qtwidgets class worker(qtcore.qthread): datasent = qtcore.pyqtsignal(dict) def __init__(self, parent=none): super(worker, self).__init__(parent) self._stopped = true self._mutex = qtcore.qmutex() def stop(self): self._mutex.lock() self._stopped = true self._mutex.unlock() def run(self): self._stopped = false count in range(10): if self._stopped: break self.sleep(1) data = { 'message':'running %d [%d]' % ( count, qtcore.qthread.currentthreadid()), 'time': qtcore.qtime.currenttime(), 'items': [1, 2, 3], } self.datasent.emit(data) class window(qtwidgets.qwidget): def __init__(self): super(window, self).__init__() self.edit = qtwidgets.qplaintextedit() self.edit.setreadonly(true) self.button = qtwidgets.qpushbutton('start') self.button.clicked.connect(self.handlebutton) layout = qtwidgets.qvboxlayout(self) layout.addwidget(self.edit) layout.addwidget(self.button) self._worker = worker() self._worker.started.connect(self.handlethreadstarted) self._worker.finished.connect(self.handlethreadfinished) self._worker.datasent.connect(self.handledatasent) def handlethreadstarted(self): self.edit.clear() self.button.settext('stop') self.edit.appendplaintext('started') def handlethreadfinished(self): self.button.settext('start') self.edit.appendplaintext('stopped') def handledatasent(self, data): self.edit.appendplaintext('message [%d]' % qtcore.qthread.currentthreadid()) self.edit.appendplaintext(data['message']) self.edit.appendplaintext(data['time'].tostring()) self.edit.appendplaintext(repr(data['items'])) def handlebutton(self): if self._worker.isrunning(): self._worker.stop() else: self._worker.start() if __name__ == '__main__': app = qtwidgets.qapplication(sys.argv) window = window() window.setgeometry(500, 100, 400, 400) window.show() sys.exit(app.exec_())
Comments
Post a Comment