PyQt,QThread,GIL,GUI [英] PyQt, QThread, GIL, GUI

查看:257
本文介绍了PyQt,QThread,GIL,GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用Python编写的GUI和程序逻辑.我经常通过调用urllib.requests(以此类推)从Web请求信息,这在GUI无响应但此调用用QThread包装时引起了问题.我认为这是由于GIL导致的.但是如何在PyQt应用程序中使用QThread呢?如果我不能使代码异步工作,怎么在PyQt中使用呢?

I have GUI and program logic written in Python. I am requesting information from web by calling urllib.requests (and so on) very often and this cause a problem when GUI is unresponsive but this calls are wrapped with QThread. I think that happens because of GIL. But how when I can use QThread in PyQt application, what use of it in PyQt if I can't make code to work asynchronously?

-代码-

qtthreaddecorator.py:

from PyQt4 import QtCore

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

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

        self._finished_slot = finished_slot

    def run(self):
        self._function(*self._args, **self._kwargs)

        self._finished_slot()

        return

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

            return
        return wrapper
    return 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(self._fill_servers)
    def _get_servers():
        self._get_my_servers()
    @qtthreaddecorator.qt_thread_decorator(self._fill_user_info)
    def _get_user_info():
        self._get_user_info()

    _get_servers()
    _get_user_info()

就我而言,_get_servers()_get_user_info()按顺序调用,但我想同时执行它们.

In my case, _get_servers() and _get_user_info() calls in order but I want to execute them concurrently.

推荐答案

我认为您在使用装饰器方面过于复杂.您可以使用大约3-4行设置代码轻松地将代码包装在新线程中.另外,我不认为您应该直接从另一个线程调用完成的插槽.您应该使用连接的信号将其激活.

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_())

这篇关于PyQt,QThread,GIL,GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆