PySide2 QThread冻结了UI

时间:2019-07-17 20:57:02

标签: python multithreading pyside2

我正在尝试使用Qt在后台运行长时间的阻塞呼叫。 (确切地说,是pyattyscomm.AttysScanscan方法。)我认为我应该使用QThread,并提出以下建议:

import pyattyscomm
from PySide2.QtCore import QThread, Signal


class DataCollectionThread(QThread):
    attys_ready = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.scanner = pyattyscomm.AttysScan()

    def run(self):
        self.scanner.scan() # this is a long, blocking call
        self.attys_ready.emit()

但是,当我start线程(来自小部件中的某些逻辑)时,GUI挂起,直到scan方法完成(即一段时间)。

我用scan替换了对time.sleep(10)的调用,这很奇怪,它没有冻结UI。我需要/可以更改一些内容来避免scan阻塞吗?

1 个答案:

答案 0 :(得分:0)

借助Grzegorz Bokota的帮助,我了解到C ++库没有释放GIL(与其他C / C ++绑定(如OpenCV和NumPy)不同)。

要解决此问题,我在'-threads'submitted the patch to upstream.swig_opts数组中添加了setup.py

此补丁发布为version 1.3.3.2 of pyattyscomm.