Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Pycharm debugger not stopping at a breakpoint outside the main thread? [duplicate]

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()
like image 367
R01k Avatar asked Jan 28 '26 13:01

R01k


1 Answers

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)

like image 104
R01k Avatar answered Jan 30 '26 03:01

R01k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!