Search code examples
pythonmultithreadingpyside2

PySide2 QThread is freezing the UI


I'm trying to run a long, blocking call in the background with Qt. (To be exact, the scan method for pyattyscomm.AttysScan.) I thought I should use QThread, and came up with the following:

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

However, when I start the thread (from some logic in a widget), the GUI hangs until the scan method finishes (i.e. a while.)

I replaced the call to scan with time.sleep(10), which strangely did not freeze the UI. Is there something I need to/can change to avoid scan blocking?


Solution

  • Thanks to the help of Grzegorz Bokota, I learned that the C++ libraries weren't releasing the GIL (unlike other C/C++ bindings like OpenCV and NumPy.)

    To fix this, I added '-threads' to the swig_opts array in setup.pyand submitted the patch to upstream.

    This patch was released as version 1.3.3.2 of pyattyscomm.