VScode, Python 如何多线程 debug 代码

2023-03-23 20:48:34 +08:00
 wganbleuthall
        .........
        self._process = subprocess.Popen(
            exec_cmd, universal_newlines=True, startupinfo=startupinfo
        )
        self.started = True
        
        exec_cmd='path_to/activitywatch/venv/bin/aw-watcher-window'

VScode 中 debug 的时候,subprocess.Popen执行完了,到下一句,都没有断在 aw-watcher-window 的main中,断点肯定是打了的 不知道如何才能在 VScode 中 debug 这种多线程的 python 代码

launch.json 配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "subProcess": true,
            "justMyCode": true
        },
        {
            "name": "MySubProcess",
            "type": "python",
            "request": "attach",
            "processId":"${command:pickProcess}",
            "justMyCode": true
        }
    ]
}
3310 次点击
所在节点    Visual Studio Code
17 条回复
apake
2023-03-23 20:58:44 +08:00
print 大法
wganbleuthall
2023-03-23 21:03:14 +08:00
@apake #1 怎么可能... 太笨了这法子
ch2
2023-03-23 21:25:45 +08:00
多线程只能 log ,把变量全打印出来
shinonome
2023-03-23 22:48:05 +08:00
我也想知道,之前发现 bebug 无效
pcbl
2023-03-23 23:05:24 +08:00
print 大法好用的很
print(11111111111111111)
print(22222222222222222)
print(33333333333333333)
既解压还好辨认,一大堆输出里面一眼就能看出哪个有哪个没有
yinmin
2023-03-24 00:23:46 +08:00
多线程是可以设置断点的。但是,你这是多进程,不是多线程。
Alias4ck
2023-03-24 00:53:37 +08:00
Alias4ck
2023-03-24 00:59:30 +08:00
https://code.visualstudio.com/docs/python/debugging 搜 multithread 关键词 它有一个场景 如果你用的是 win32api 创建线程的话 而不是 python 内部的话 你则需要加上一段话

If you're working with a multi-threaded app that uses native thread APIs (such as the Win32 CreateThread function rather than the Python threading APIs), it's presently necessary to include the following source code at the top of whichever file you want to debug:
```
import debugpy
debugpy.debug_this_thread()
```
duduke
2023-03-24 08:10:52 +08:00
python 这傻鸟真的难用
ALLROBOT
2023-03-24 11:46:05 +08:00
用 pytest 大法
t133
2023-03-24 11:48:26 +08:00
就我觉得这个 subprocess 不是 Python 管理的嘛?
wganbleuthall
2023-03-24 11:48:55 +08:00
@yinmin #6 是啊 我后来才意识到不是线程,是进程,也是很少玩 python ,只能一个进程一个进程的启动来 debug 了
wganbleuthall
2023-03-24 11:50:46 +08:00
@t133 #11 ...是我弄错了 pycharm 搞起来也没法 debug 才突然发现是进程,很少玩 python...
yinmin
2023-03-24 12:53:02 +08:00
@wganbleuthall 如果子进程也是 python 代码,建议改成子线程 ThreadPoolExecutor 。ThreadPoolExecutor 可以配置一个最大线程数,例如:你设置最大线程数是 5 ,你可以一次性把 100 个任务都仍到 ThreadPoolExecutor 里运行,ThreadPoolExecutor 会先同时运行前 5 个,然后结束 1 个再运行队列的下一个,直到 100 个任务都运行完毕。

ThreadPoolExecutor 里运行的代码是支持 debug 的。
yinmin
2023-03-24 13:04:55 +08:00
如果必须是子进程,我是这样调试的。
1. 把子进程的核心代码封装成 class ,通过代码直接引用的方式,把子进程的 class 加到主进程项目里直接调用
2. 调试去 bug
3. 将 class 从主进程移除,改回到子进程,加入一些 print
4. 主进程通过 Popen 调用子进程,看一下 print 出来的内容是否 OK
kaddusabagei38
2023-03-24 15:57:23 +08:00
打不了断点就别硬打了,总有人很排斥 print 调试法,我也排斥,但是有那时间解决这问题还不如直接几行 print 或者 logger 结束了,不要把有限的时间浪费在无解的问题上。
lysS
2023-03-24 17:06:29 +08:00
我的 golang 是可以的,堆栈里面有所有 goroutine

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/926645

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX