Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python asynchronous/threads debugging in Visual Studio Code

I'm using Visual Studio Code for writing my python application. Inspecting variables and things like setting a watch in the left debugging pane works OK if I write a simple synchronous one-file program.

However, I have code with classes in multiple files, and use various callbacks from other modules. Some which start a new thread.

One of those modules is canopen I can step thru my code, but when I enter the second line (below)

can0 = canopen.Network() can0.connect(channel='can0', bustype='socketcan') 

then the call stack changes from:

CALL STACK paused on breakpoint main <module> 

to

CALL STACK paused on breakpoint MainThread Thread#15034......... 

and simultaneously

  • the variables pane clears
  • and the watches in my watch window shows:

    can0: not available

How can I (setup VS studio code with Python to) inspect/debug my python code with various threads and code in various files?

Regards, Bas

like image 463
luminize Avatar asked Apr 23 '17 08:04

luminize


People also ask

How do I debug multiple threads in Visual Studio?

Yes. In the Threads window (Debug -> Windows -> Threads) right-click the thread you want and select "switch to thread". You can also choose "freeze" on the threads you don't want to debug in order to keep them from running. Don't forget to "thaw" them if you expect them to do work, however.

Can we debug async method?

Debug asynchronous code Debugging asynchronous code is a challenge because the tasks are often scheduled in one thread and executed in another. Every thread has its own stacktrace, making it difficult to figure out what happened before the thread started.

How do I debug Python code in Visual Studio?

Python in Visual Studio supports debugging without a project. With a stand-alone Python file open, right-click in the editor, select Start with Debugging, and Visual Studio launches the script with the global default environment (see Python environments) and no arguments.

How do I view threads in VS Code?

To display the Threads window in break mode or run mode While Visual Studio is in debug mode, select the Debug menu, point to Windows, and then select Threads.


1 Answers

New debugger is capable of debugging async applications. Check out how to set it up How to update ptvsd used by Visual Studio Code in debug mode And don't forget to add "subProcess": true, in launch.json

like image 177
Maks Avatar answered Oct 14 '22 08:10

Maks