V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wganbleuthall
V2EX  ›  Visual Studio Code

VScode, Python 如何多线程 debug 代码

  •  
  •   wganbleuthall · 2023-03-23 20:48:34 +08:00 · 3237 次点击
    这是一个创建于 371 天前的主题,其中的信息可能已经有所发展或是发生改变。
            .........
            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
            }
        ]
    }
    
    17 条回复    2023-03-24 17:06:29 +08:00
    apake
        1
    apake  
       2023-03-23 20:58:44 +08:00 via Android
    print 大法
    wganbleuthall
        2
    wganbleuthall  
    OP
       2023-03-23 21:03:14 +08:00
    @apake #1 怎么可能... 太笨了这法子
    ch2
        3
    ch2  
       2023-03-23 21:25:45 +08:00
    多线程只能 log ,把变量全打印出来
    shinonome
        4
    shinonome  
       2023-03-23 22:48:05 +08:00
    我也想知道,之前发现 bebug 无效
    pcbl
        5
    pcbl  
       2023-03-23 23:05:24 +08:00
    print 大法好用的很
    print(11111111111111111)
    print(22222222222222222)
    print(33333333333333333)
    既解压还好辨认,一大堆输出里面一眼就能看出哪个有哪个没有
    yinmin
        6
    yinmin  
       2023-03-24 00:23:46 +08:00
    多线程是可以设置断点的。但是,你这是多进程,不是多线程。
    Alias4ck
        8
    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
        9
    duduke  
       2023-03-24 08:10:52 +08:00 via iPhone
    python 这傻鸟真的难用
    ALLROBOT
        10
    ALLROBOT  
       2023-03-24 11:46:05 +08:00
    用 pytest 大法
    t133
        11
    t133  
       2023-03-24 11:48:26 +08:00 via iPhone
    就我觉得这个 subprocess 不是 Python 管理的嘛?
    wganbleuthall
        12
    wganbleuthall  
    OP
       2023-03-24 11:48:55 +08:00
    @yinmin #6 是啊 我后来才意识到不是线程,是进程,也是很少玩 python ,只能一个进程一个进程的启动来 debug 了
    wganbleuthall
        13
    wganbleuthall  
    OP
       2023-03-24 11:50:46 +08:00
    @t133 #11 ...是我弄错了 pycharm 搞起来也没法 debug 才突然发现是进程,很少玩 python...
    yinmin
        14
    yinmin  
       2023-03-24 12:53:02 +08:00   ❤️ 1
    @wganbleuthall 如果子进程也是 python 代码,建议改成子线程 ThreadPoolExecutor 。ThreadPoolExecutor 可以配置一个最大线程数,例如:你设置最大线程数是 5 ,你可以一次性把 100 个任务都仍到 ThreadPoolExecutor 里运行,ThreadPoolExecutor 会先同时运行前 5 个,然后结束 1 个再运行队列的下一个,直到 100 个任务都运行完毕。

    ThreadPoolExecutor 里运行的代码是支持 debug 的。
    yinmin
        15
    yinmin  
       2023-03-24 13:04:55 +08:00
    如果必须是子进程,我是这样调试的。
    1. 把子进程的核心代码封装成 class ,通过代码直接引用的方式,把子进程的 class 加到主进程项目里直接调用
    2. 调试去 bug
    3. 将 class 从主进程移除,改回到子进程,加入一些 print
    4. 主进程通过 Popen 调用子进程,看一下 print 出来的内容是否 OK
    kaddusabagei38
        16
    kaddusabagei38  
       2023-03-24 15:57:23 +08:00
    打不了断点就别硬打了,总有人很排斥 print 调试法,我也排斥,但是有那时间解决这问题还不如直接几行 print 或者 logger 结束了,不要把有限的时间浪费在无解的问题上。
    lysS
        17
    lysS  
       2023-03-24 17:06:29 +08:00
    我的 golang 是可以的,堆栈里面有所有 goroutine
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:44 · PVG 05:44 · LAX 14:44 · JFK 17:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.