本文介绍了在 PyQt4 中使用 QThreadPool 和 QRunnable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码片段

class Worker(QtCore.QRunnable):
    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
        print('Running Worker')

class Tasks(QtCore.QObject):
    def __init__(self):
        super(Tasks, self).__init__()
        self.pool = QtCore.QThreadPool.globalInstance()
        self.pool.setMaxThreadCount(2)

    def start(self):
        for task in range(3):
            worker = Worker()
            self.pool.start(worker)
        self.pool.waitForDone()

def main():
    prefix_path = os.environ['QGIS_PREFIX_PATH']
    QgsApplication.setPrefixPath(prefix_path, True)
    QgsApplication.initQgis()
    tasks = Tasks()
    tasks.start()

我收到以下错误

_original_runnable_init(self, *args, **kwargs)
TypeError: keyword arguments are not supported

创建工作对象有什么问题?

What is wrong with Worker Object creation?

推荐答案

不知道 QGis 初始化有什么问题,但以下代码在我的机器上按预期工作:

Don't know what is wrong with the QGis initialization, but the following code works as expected in my machine:

from PyQt4 import QtCore

class Worker(QtCore.QRunnable):
    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
        print('Running Worker')

class Tasks(QtCore.QObject):
    def __init__(self):
        super(Tasks, self).__init__()
        self.pool = QtCore.QThreadPool.globalInstance()
        self.pool.setMaxThreadCount(2)

    def start(self):
        for task in range(3):
            worker = Worker()
            self.pool.start(worker)
        self.pool.waitForDone()

if __name__ == '__main__':
    tasks = Tasks()
    tasks.start()

它给了我以下输出:

Running Worker
Running Worker
Running Worker

这篇关于在 PyQt4 中使用 QThreadPool 和 QRunnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 04:02