如何覆盖 PyQt5 QThreadPool?

Posted

技术标签:

【中文标题】如何覆盖 PyQt5 QThreadPool?【英文标题】:How to cover PyQt5 QThreadPool? 【发布时间】:2021-01-11 21:30:58 【问题描述】:

如何覆盖 PyQt5 QThreadPool ?有可能吗?

在这篇文章https://www.learnpyqt.com/tutorials/multithreading-pyqt-applications-qthreadpool/ 之后,可以将 PyQt5 与线程池一起使用。这是一个稍微改编的代码(以启用覆盖):

~> more .\pyqt_multithread.py
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")
        self.b = QPushButton("DANGER!")
        self.b.pressed.connect(self.oh_no)

        layout.addWidget(self.l)
        layout.addWidget(self.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):
        print("THREAD CREATED")
        for n in range(0, 5):
            time.sleep(1)
            progress_callback.emit(int(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)


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    app.exec_()

现在为了覆盖这段代码,我创建了一个单元测试:

~> more .\tst.py
import unittest

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtTest import QTest
from pyqt_multithread import MainWindow

import functools

def stopTest(window):
    window.close()

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        app = QApplication([])
        window = MainWindow()
        timerCallback = functools.partial(stopTest, window)
        timer = QTimer()
        timer.timeout.connect(timerCallback)
        timer.start(10000)
        QTest.mouseClick(window.b, Qt.LeftButton)
        QTest.mouseClick(window.b, Qt.LeftButton)
        QTest.mouseClick(window.b, Qt.LeftButton)
        QTest.mouseClick(window.b, Qt.LeftButton)
        app.exec_()

if __name__ == '__main__':
    unittest.main()


但覆盖范围无关紧要:代码的某些部分(166-121 是线程回调)没有被覆盖,除非它们被覆盖


~> python -m coverage run --concurrency=thread tst.py
Multithreading with maximum 4 threads
THREAD CREATED
THREAD CREATED
THREAD CREATED
THREAD CREATED
0% done
0% done
0% done
0% done
25% done
25% done
25% done
25% done
50% done
50% done
50% done
50% done
75% done
75% done
75% done
75% done
100% done
Done.
THREAD COMPLETE!
100% done
Done.
THREAD COMPLETE!
100% done
Done.
THREAD COMPLETE!
100% done
Done.
THREAD COMPLETE!
.
----------------------------------------------------------------------
Ran 1 test in 10.916s

OK


~> python -m coverage report -m
Name                  Stmts   Miss  Cover   Missing
---------------------------------------------------
pyqt_multithread.py      73     16    78%   67-76, 116-121, 146-148
tst.py                   24      0   100%
---------------------------------------------------
TOTAL                    97     16    84%

在覆盖 PyQt5 代码时如何获得相关覆盖率?

【问题讨论】:

【参考方案1】:

Coverage (--concurrency=thread) 仅适用于高级线程模块:https://coverage.readthedocs.io/en/coverage-4.3.3/trouble.html。将QThreadPool 替换为threading.Threads 池可以获取相关覆盖率。

【讨论】:

以上是关于如何覆盖 PyQt5 QThreadPool?的主要内容,如果未能解决你的问题,请参考以下文章

pyqt5注意事项

PyQt5 - 覆盖鼠标光标并设置 Qwidget 透明以一起输入

在 QThreadPool 中执行槽

如何在 PyQt5 中反转旋转框选择?

将 MuparserX 与 QThreadPool 一起使用时程序崩溃

QThreadPool 强制停止在 qt cpp