Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded python application not hitting breakpoints in Visual Studio

I am currently developing a PyQt application in Visual Studio. Debugging has been working great, until I decided to keep my UI responsive by moving stuff to a worker thread with Qt Threads.

class MainWindow(base, form):

    start_work = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        # Create a seperate thread in which the update information is polled.
        self.thread = QtCore.QThread()
        # Create Worker object and move it to new thread
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        # connect signal to start work in the extra tread
        self.start_work.connect(self.worker.get_work_done)
        self.thread.start()

    #function emit a signal to start doing the work
    def do_work(self):
        self.startWork.emit()

Any function that is invoked on my worker object is connected via signal slots

class Worker(QtCore.QObject):
    @QtCore.pyqtSlot()
    def get_work_done(self):
        #lets do some time consuming work.

The code works fine. The only problem is now, I cannot debug anything that is happening inside get_work_done. Visual studio won't break at those breakpoints.

When I break inside any MainWindow function, the Visual Studio Debugger shows only one thread. It seems unaware of any other threads created by the application.

like image 615
kiki Avatar asked Apr 14 '15 19:04

kiki


2 Answers

As said already all threads not created using Python need to be registered with PTVS. To do this for a QThread, call this in the threads run method:

import pydevd;pydevd.settrace(suspend=False)

Some more info here: Can I put break points on background threads in Python?

like image 63
ichundes Avatar answered Nov 15 '22 03:11

ichundes


Debugger needs to play some tricks to detect new threads and set up its hooks (which are needed to hit breakpoints etc). It does so by hijacking the standard Python _thread module. If you're creating the threads in some way that circumvents that module altogether, which is what I suspect Qt does here, the debugger will not be aware of those threads.

Try using the standard threading module instead of QThread, and see if that helps.

like image 32
Pavel Minaev Avatar answered Nov 15 '22 03:11

Pavel Minaev