wganbleuthall

VScode, Python 如何多线程 debug 代码

  •  
  •   wganbleuthall · Mar 23, 2023 · 4877 views
    This topic created in 1158 days ago, the information mentioned may be changed or developed.
            .........
            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 replies    2023-03-24 17:06:29 +08:00
    apake
        1
    apake  
       Mar 23, 2023 via Android
    print 大法
    wganbleuthall
        2
    wganbleuthall  
    OP
       Mar 23, 2023
    @apake #1 怎么可能... 太笨了这法子
    ch2
        3
    ch2  
       Mar 23, 2023
    多线程只能 log ,把变量全打印出来
    shinonome
        4
    shinonome  
       Mar 23, 2023
    我也想知道,之前发现 bebug 无效
    pcbl
        5
    pcbl  
       Mar 23, 2023
    print 大法好用的很
    print(11111111111111111)
    print(22222222222222222)
    print(33333333333333333)
    既解压还好辨认,一大堆输出里面一眼就能看出哪个有哪个没有
    yinmin
        6
    yinmin  
       Mar 24, 2023
    多线程是可以设置断点的。但是,你这是多进程,不是多线程。
    Alias4ck
        8
    Alias4ck  
       Mar 24, 2023
    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  
       Mar 24, 2023 via iPhone
    python 这傻鸟真的难用
    ALLROBOT
        10
    ALLROBOT  
       Mar 24, 2023
    用 pytest 大法
    t133
        11
    t133  
       Mar 24, 2023 via iPhone
    就我觉得这个 subprocess 不是 Python 管理的嘛?
    wganbleuthall
        12
    wganbleuthall  
    OP
       Mar 24, 2023
    @yinmin #6 是啊 我后来才意识到不是线程,是进程,也是很少玩 python ,只能一个进程一个进程的启动来 debug 了
    wganbleuthall
        13
    wganbleuthall  
    OP
       Mar 24, 2023
    @t133 #11 ...是我弄错了 pycharm 搞起来也没法 debug 才突然发现是进程,很少玩 python...
    yinmin
        14
    yinmin  
       Mar 24, 2023   ❤️ 1
    @wganbleuthall 如果子进程也是 python 代码,建议改成子线程 ThreadPoolExecutor 。ThreadPoolExecutor 可以配置一个最大线程数,例如:你设置最大线程数是 5 ,你可以一次性把 100 个任务都仍到 ThreadPoolExecutor 里运行,ThreadPoolExecutor 会先同时运行前 5 个,然后结束 1 个再运行队列的下一个,直到 100 个任务都运行完毕。

    ThreadPoolExecutor 里运行的代码是支持 debug 的。
    yinmin
        15
    yinmin  
       Mar 24, 2023
    如果必须是子进程,我是这样调试的。
    1. 把子进程的核心代码封装成 class ,通过代码直接引用的方式,把子进程的 class 加到主进程项目里直接调用
    2. 调试去 bug
    3. 将 class 从主进程移除,改回到子进程,加入一些 print
    4. 主进程通过 Popen 调用子进程,看一下 print 出来的内容是否 OK
    kaddusabagei38
        16
    kaddusabagei38  
       Mar 24, 2023
    打不了断点就别硬打了,总有人很排斥 print 调试法,我也排斥,但是有那时间解决这问题还不如直接几行 print 或者 logger 结束了,不要把有限的时间浪费在无解的问题上。
    lysS
        17
    lysS  
       Mar 24, 2023
    我的 golang 是可以的,堆栈里面有所有 goroutine
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3509 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    ♥ Do have faith in what you're doing.