如何将参数传递给运行函数的PyQt QThreadPool?

4

关于这段代码的问题:使用 QThreadPool 在 PyQt 应用程序中进行多线程

从下面的代码来看,如何从 oh_no 函数向 execute_this_fn 传递附加参数?

如果我直接传递参数:

worker = Worker(self.execute_this_fn(arg1, arg2))

并像往常一样接受:
def execute_this_fn(self, progress_callback, a ,b):

我遇到了一个错误:

result = self.fn(*self.args, **self.kwargs) TypeError: execute_this_fn()的参数'progress_callback'被传递了多个值

甚至有评论说“把任何args和kwargs传递给run函数”,但正确的语法是什么?

文章中的可执行代码:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import time
import traceback, sys


class WorkerSignals(QObject):
    '''
    Defines the signals available from a running worker thread.

    Supported signals are:

    finished
        No data

    error
        `tuple` (exctype, value, traceback.format_exc() )

    result
        `object` data returned from processing, anything

    progress
        `int` indicating % progress 

    '''
    finished = pyqtSignal()
    error = pyqtSignal(tuple)
    result = pyqtSignal(object)
    progress = pyqtSignal(int)


class Worker(QRunnable):
    '''
    Worker thread

    Inherits from QRunnable to handler worker thread setup, signals and wrap-up.

    :param callback: The function callback to run on this worker thread. Supplied args and 
                     kwargs will be passed through to the runner.
    :type callback: function
    :param args: Arguments to pass to the callback function
    :param kwargs: Keywords to pass to the callback function

    '''

    def __init__(self, fn, *args, **kwargs):
        super(Worker, self).__init__()

        # Store constructor arguments (re-used for processing)
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.signals = WorkerSignals()    

        # Add the callback to our kwargs
        self.kwargs['progress_callback'] = self.signals.progress        

    @pyqtSlot()
    def run(self):
        '''
        Initialise the runner function with passed args, kwargs.
        '''

        # Retrieve args/kwargs here; and fire processing using them
        try:
            result = self.fn(*self.args, **self.kwargs)
        except:
            traceback.print_exc()
            exctype, value = sys.exc_info()[:2]
            self.signals.error.emit((exctype, value, traceback.format_exc()))
        else:
            self.signals.result.emit(result)  # Return the result of the processing
        finally:
            self.signals.finished.emit()  # Done



class MainWindow(QMainWindow):


    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.counter = 0

        layout = QVBoxLayout()

        self.l = QLabel("Start")
        b = QPushButton("DANGER!")
        b.pressed.connect(self.oh_no)

        layout.addWidget(self.l)
        layout.addWidget(b)

        w = QWidget()
        w.setLayout(layout)

        self.setCentralWidget(w)

        self.show()

        self.threadpool = QThreadPool()
        print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())

        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.recurring_timer)
        self.timer.start()

    def progress_fn(self, n):
        print("%d%% done" % n)

    def execute_this_fn(self, progress_callback):
        for n in range(0, 5):
            time.sleep(1)
            progress_callback.emit(n*100/4)

        return "Done."

    def print_output(self, s):
        print(s)

    def thread_complete(self):
        print("THREAD COMPLETE!")

    def oh_no(self):
        # Pass the function to execute
        worker = Worker(self.execute_this_fn) # Any other args, kwargs are passed to the run function
        worker.signals.result.connect(self.print_output)
        worker.signals.finished.connect(self.thread_complete)
        worker.signals.progress.connect(self.progress_fn)

        # Execute
        self.threadpool.start(worker) 


    def recurring_timer(self):
        self.counter +=1
        self.l.setText("Counter: %d" % self.counter)


app = QApplication([])
window = MainWindow()
app.exec_()

你明白使用ThreadPool和使用QThread的区别吗?我问这个问题是因为它们用于非常不同的目的。 - Dennis Jensen
@DennisJensen QThreadPool负责为我处理队列和执行,但是QThread需要指示排队和加入线程吗? - Nick PV
@NickPV 1) 我在这里不是来交际的,所以我避免发表自己是否被喜欢的意见。2) 根据SO规则,MRE必须在问题中,并且不能依赖于外部资源,因为如果您提供的链接失效,问题可能无法理解。现在你明白我的请求了吗? - eyllanesc
@eyllanesc 对不起,我明白了,现在已经有了。对于这个问题有什么想法吗? - Nick PV
是的,完美,它正在工作。现在我理解这意味着什么了:self.kwargs["progress_callback"] = self.signals.progress - 进度回调属于kwargs,但同时也是函数的一个信号。很好,谢谢! - Nick PV
显示剩余2条评论
1个回答

4
如果您查看Worker类的文档,我们会发现它说明了如何建立额外的参数:
class Worker(QRunnable):
    """
    Worker thread

    Inherits from QRunnable to handler worker thread setup, signals and wrap-up.

    :param callback: The function callback to run on this worker thread. Supplied args and 
                     kwargs will be passed through to the runner.
    :type callback: function
    :param args: Arguments to pass to the callback function
    :param kwargs: Keywords to pass to the callback function
    # ...

在您的情况下,您必须更改为:

worker = Worker(self.execute_this_fn, arg1, arg2)

另一方面,如果观察到源代码的以下部分:

# ...
self.kwargs["progress_callback"] = self.signals.progress
# ...

由此可见,progress_callback是必须放在args之后的kwargs中,因此您需要更改您的代码为:
def execute_this_fn(self, a, b, progress_callback):

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接