4

I'm using PyCharm 2016.2.3 (Build #PY-162.1967.10). I can't get the debugger to stop at any breakpoint out of the main thread no matter is the suspend property for the breakpoint is All or Thread; or if that is the only breakpoint in the whole program. The threading is implemented with a QObject moved into a QThread. This is a simple code to show the problem. A breakpoint anywhere in the secondary thread (inside Master.do() or Worker.run()) will not be hit.

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()
R01k
  • 735
  • 3
  • 12
  • 26

1 Answers1

3

Found the answer here: Not working python breakpoints in C thread in pycharm or eclipse+pydev

Basically, I had to import pydevd and add the following in the thread right after starting it: pydevd.settrace(suspend=False, trace_only_current_thread=True)

Community
  • 1
  • 1
R01k
  • 735
  • 3
  • 12
  • 26