Debug python multiprocess program with VSCode

How to debug a program using python's multiprocessing module on VSCode

Assumed reader

--The python code using the multiprocessing module works fine on the console, but VSCode Those who say that it doesn't work when I try to debug with --Those who want to understand python's multi-process program but don't understand without a debugger --When debugging with VS Code ↓ Those who are in trouble with such an error

Exception escaped from start_client  

failed to launch debugger for child process  

AssertionError: can only join a child process  

RuntimeError: already started

How to do

Debugging a program using the multiprocessing module requires the following three points:

・ 1. If \ _ \ _ name__ == "\ _ \ _ main__": Write

If \ _ \ _ name \ _ \ _ == "\ _ \ _ main \ _ \ _": is required for the file (top level module) to start debugging. If you don't see this statement, add it to format your code. It can be a daunting task in some cases, but it is essential.

・ 2. Write the statement multiprocessing.set_start_method ('spawn', True)

For example, writing the following directly under the main statement will work.

Description example


if __name__ == "__main__": 
    import multiprocessing 
    multiprocessing.set_start_method('spawn', True)

For windows freeze_support()
It may be necessary to add a line

・ 3. Write the section'"subProcess": true'in launch.json

Press Ctrl + Shift + D to enter debug mode and edit the configuration launch.json file.

openjson.jpg

Add a section called "subProcess": true here.

Configuration example


{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "subProcess": true,    //Postscript
      "console": "integratedTerminal"
    }
  ]
}

Debugging the jupyter notebook file (.ipynb)

VSCode can open and run jupyter notebook files (.ipynb). However, there is still no ability to debug open code. If you want to debug a multi-process program, please apply the above method after converting it to a python script with VSCode function.

Confirmed environment

OS: Debian 10 python: 3.7.4 Visual Studio Code: 1.44.0

Recommended Posts

Debug python multiprocess program with VSCode
Multi-process asynchronously with python
Debug Python with VS Code
Debug with PEPPER python interpreter
Debug shared libraries with VScode
I want to debug with Python
[Python] A program that creates stairs with #
[Python] View dataframe in VScode debug console
2D FEM stress analysis program with Python
HTML document your Python program with Sphinx
Debug for mysql connection with python mysql.connector
[Python] Shopping program
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Python] About multi-process
Excel with Python
Microcomputer with Python
Cast with python
Debug with VS Code using boost python numpy
[python] [vscode] When you get angry with space-tab-mixed
Sample program that outputs syslog with Python logging
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
[Note] Anaconda & VScode has trouble with python import error
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer