gpt4 book ai didi

python - 为什么在 VS Code 的集成 Python 调试中启动多个调试线程?

转载 作者:行者123 更新时间:2023-12-03 12:47:38 24 4
gpt4 key购买 nike

** 更新 **
来自 a related discussion我得到了以下答案:

Also, something to double check in your code is if the main entry point is protected by the __name__=='__main__' check:

if __name__ == '__main__':
main()

and depending on the structure, you could also need the multiprocessing.freeze_support() if you need to have multiprocessing support:

i.e.:

if __name__ == '__main__':
multiprocessing.freeze_support()
main()

(if you don't have that structure, it may explain why multiprocessing is executing your main entry point code multiple times).


然而,我既没有实现上述第一个选项,也没有实现第二个上述选项,而且,我执行的任何脚本都可能发生这种突然的多线程行为。
似乎旧的/以前的调试 session 并没有真正在幕后终止,因为在一个 session 中没有多线程,然后在调试时启动多 session 。
正常执行代码时也会发生这种情况,它以错误退出,然后我(再次)进入调试 session 。
我不知道如何实现它,但我应该把
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
在我作为主脚本执行的所有 python 脚本的开头,即我的入口点或顶级脚本?
** 原始问题 **
正如在 中所见附截图 ,同时按 F5进行标准调试过程在 VS Code (之前定义了一些断点),偶尔会发生以下情况:
Python debugging in VS Code
这会导致大量额外的计算时间、滞后和冗余的控制台输出,因为同一代码连续执行多次。
奇怪的是,这种行为似乎是随机发生的。只有有时我才能得出结论,我之前尝试启动一个调试 session ,该 session 在代码中的某个点被 Uncaught Exception 中断。 ,但对我来说为什么 VS Code 似乎仍然不合逻辑当按下 F5 时,应该重新启动所有这些以前失败的调试 session 以及一个新的调试 session 。 .
通过 Shift+F5 中断调试程序时或红色方形按钮 Stop ,有时消息 timeout after 1000 ms出现在 VS Code 的右下角- window ;尤其是当问题发生时,在 call stack 中显示了多个不需要的子进程和线程。 .
我的相关部分 launch.json - 定义集成终端调试的文件是:
{
// !!THIS CONFIG-FILE IS USED FOR DEBUGGING!!
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
// VS Code Integrated Terminal. If redirectOutput is set to True, output is also displayed in the debug console.
"console": "integratedTerminal",
// Make sure that code output is also being displayed in the debug console.
"redirectOutput": true,
// When omitted or set to true (the default), restricts debugging to user-written code only. Set to false to also enable debugging of standard library functions.
"justMyCode": false
},
...
至于 settings.json - 文件,除了通用选项 "debug.allowBreakpointsEverywhere": true 之外,我还没有实现任何有关 python 调试的选项。 .
我想了解为什么这种不受欢迎的调试行为首先出现并最终能够防止它发生。

PS 试图在 official GitHub of VS Code 上获得答案:
我已经提交了 issue on the VS Code GitHub-page ,但被建议在 StackOverflow 上询问,因为

"VS Code only shows what is being requested by the Python debugger."

最佳答案

您的代码是多线程的还是您过去调试的这些进程(但这些进程没有按预期被终止)?
我个人在通过 Run -> Start Debugging 时没有观察到这个问题 - 我认为这与 F5 相同。
这就是我的调用堆栈部分以及我的 launch.json 的样子。完成调试 session 后,请确保点击停止或断开连接按钮。
enter image description here

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}

关于python - 为什么在 VS Code 的集成 Python 调试中启动多个调试线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63011957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com