PyQt, QThread, GIL, GUI

五迷三道 提交于 2019-12-01 08:51:14

I think you are over-complicating with use of decorators. You can easily wrap your code in new thread using about 3-4 lines of setup code. Also I do not think you should call your finished slot directly from another thread. You should use a connected signal to activate it.

import sys
from time import sleep
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Signals(QObject):
    update = pyqtSignal(int)
    enable_button = pyqtSignal(bool)

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.button = QPushButton("Run", self)
        self.button.clicked.connect(self.onButton)

        self.progress = QProgressBar(self)
        self.progress.setTextVisible(False)

        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(5, 5, 5, 5)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.progress)
        self.layout.addStretch()

        self.worker_thread = QThread()
        self.worker_thread.run = self.worker
        self.worker_thread.should_close = False

        self.signals = Signals()
        self.signals.update.connect(self.progress.setValue)
        self.signals.enable_button.connect(self.button.setEnabled)

        self.setLayout(self.layout)
        self.show()
        self.resize(self.size().width(), 0)

    # Override
    def closeEvent(self, e):
        self.worker_thread.should_close = True
        self.worker_thread.wait()

    @pyqtSlot()
    def onButton(self):
        self.button.setDisabled(True)
        self.worker_thread.start()

    # Worker thread, no direct GUI updates!
    def worker(self):
        for i in range(101):
            if self.worker_thread.should_close:
                break
            self.signals.update.emit(i)
            sleep(0.1)
        self.signals.enable_button.emit(True)

app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
Victor Polevoy

Though the solution is already offered by Fenikso, it may be also interesting how to solve the problem by using decorators.

I've corrected my qtthreaddecorator.py to the following:

from PyQt4 import QtCore


class Worker(QtCore.QThread):
    threads = []

    def __init__(self, thread_name, function, *args, **kwargs):
        QtCore.QThread.__init__(self)

        self._thread_name = thread_name
        self._function = function
        self._args = args
        self._kwargs = kwargs

    def run(self):
        Worker.threads.append(self.currentThreadId())            
        self._function(*self._args, **self._kwargs)
        self.emit(QtCore.SIGNAL('finished()'))
        Worker.threads.remove(self.currentThreadId())


def qt_thread_decorator():
    def decorator(function):
        def wrapper(*args, **kwargs):
            worker = Worker(function.__name__, function, *args, **kwargs)

            def on_finish():
                worker.quit()

            worker.finished.connect(on_finish)
            worker.start()

            return worker
        return wrapper
    return decorator

And in the code which is using this decorator:

import qtthreaddecorator

class MainWindow(QtGui.QMainWindow, form_class):

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.init()

    def init(self):
        @qtthreaddecorator.qt_thread_decorator()
        def _get_servers():
            self._get_my_servers()
        @qtthreaddecorator.qt_thread_decorator()
        def _get_user_info():
            self._get_user_info()

        _get_servers().finished.connect(self._fill_servers)
        _get_user_info().finished.connect(self._fill_user_info)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!