PySide6 multithreading work incorrectly with debugger

Answered

I reported this problem in a thread of Qt Forum before. Other people also encountered this problem earlier than me (stackoverflow). 

The minimal example is attached below. Worker.run should be executed in child thread. When using Run in PyCharm, it's correct. However, when using Debug with the same configuration, it's in main thread. This piece of code is corrected by @friedemannkleint from Qt and examined several times by myself, so it should have no bug (see Qt Bug Tracker).

import sys

from PySide6.QtCore import QObject, Signal, QThread, Qt, Slot, QLibraryInfo, qVersion
from PySide6.QtWidgets import QMainWindow, QApplication


class Worker(QObject):
    update = Signal()

    def run(self):
        print(f"worker.run(): {QThread.currentThread().objectName()}")
        self.update.emit()


class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("Window")
        self._worker = Worker()
        self._worker.setObjectName("worker")
        self._thread = QThread(self)
        self._thread.setObjectName('test thread')
        self._worker.moveToThread(self._thread)
        self._thread.started.connect(self._worker.run)
        self._worker.update.connect(self.slotUpdate)
        self._thread.start()

    def closeEvent(self, e):
        self._thread.quit()
        self._thread.wait()
        e.accept()

    @Slot()
    def slotUpdate(self):
        print(f"window.update(): \"{QThread.currentThread().objectName()}\"")
        print("  window.thread()=", self.thread())
        print("  worker.thread()=", self._worker.thread())
        print("  worker.thread()=", self._thread.thread())


if __name__ == '__main__':
    print('Python {}.{}.{}'.format(sys.version_info[0], sys.version_info[1],
                                     sys.version_info[2]))
    print(QLibraryInfo.build())
    app = QApplication()
    w = Window()
    w.setWindowTitle(qVersion())
    w.show()
    sys.exit(app.exec())

Environment:

  • OS: Windows 11
  • PyCharm: 2024.1.5 Professional
  • Anaconda: 22.11.1
  • Python: 3.10
  • PySide: 6.8.1
0
3 comments

Hi Yuanxun2000 , does it start working as expected if you change PyQt compatible option to PySide under File | Settings | Build, Execution, Deployment | Python Debugger?

0

Hi Mikhail Tarabrikov, the result remains the same after I choose PySide explicitly.

0

Yuanxun2000, this issue requires thorough investigation, I created an issue here: PY-78786

To have a single discussion thread, and in order for all engineers involved in the issue to see the comments, I suggest closing this thread and continuing the discussion in the YouTrack issue. If you have any new related information, please add the comment there.  
  
To find out more about using YouTrack, please see this article

0

Please sign in to leave a comment.