为什么Pycharm调试器不在主线程外的断点处停止?

时间:2016-10-26 09:20:41

标签: multithreading debugging pyqt pycharm breakpoints

我正在使用PyCharm 2016.2.3(Build#PY-162.1967.10)。我不能让调试器在主线程的任何断点处停止,无论断点的挂起属性是All还是Thread;或者如果这是整个计划中唯一的断点。线程是在QObject移入QThread的情况下实现的。这是一个显示问题的简单代码。辅助线程中的任何位置(在Master.do()或Worker.run()内部的断点都不会被命中。

import sys
from PyQt5.QtCore import QCoreApplication, QObject, QThread, pyqtSignal, pyqtSlot


class Worker(QObject):
    """
    Generic worker.
    """
    start = pyqtSignal(str)
    finished = pyqtSignal()

    def __init__(self, function):
        QObject.__init__(self)
        self._function = function
        self.start.connect(self.run)

    @pyqtSlot()
    def run(self):
        #TODO Breakpoints inside this method will not be hit.
        self._function()
        self.finished.emit()


class Master(QObject):
    """
    An object that will use the worker class.
    """

    def __init__(self):
        QObject.__init__(self)

    def do(self):
        #TODO Breakpoints inside this method will not be hit.
        print("All done.")
        QCoreApplication.quit()


def main():
    app = QCoreApplication(sys.argv)

    master = Master()
    worker = Worker(master.do)

    thread = QThread()
    worker.moveToThread(thread)
    thread.started.connect(worker.run)

    # Terminating thread gracefully, or so.
    worker.finished.connect(thread.quit)
    worker.finished.connect(worker.deleteLater)
    thread.finished.connect(thread.deleteLater)

    thread.start()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

在这里找到答案:Not working python breakpoints in C thread in pycharm or eclipse+pydev

基本上,我必须导入pydevd并在启动后立即在线程中添加以下内容: pydevd.settrace(suspend = False,trace_only_current_thread = True)