How to debug a program using python's multiprocessing module on VSCode
--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
Debugging a program using the multiprocessing module requires the following three points:
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.
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
Press Ctrl + Shift + D to enter debug mode and edit the configuration launch.json file.
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"
}
]
}
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.
OS: Debian 10 python: 3.7.4 Visual Studio Code: 1.44.0
Recommended Posts