vscode python 多进程,多线程,带参数调试

vscode python 多进程,多线程,带参数调试,第1张

vscode python 多进程,多线程,带参数调试
  • vscode python 多进程,多线程,带参数调试
    • 多进程,多线程调试
    • 带参数调试

vscode python 多进程,多线程,带参数调试 多进程,多线程调试

高效的调试技巧可以快速找到程序中存在的问题。通常,我们可以在调试是暂停程序,查看变量等。vscode 的调试工具事实上已经提供了足够的支持。在安装完成 vscode 的 python 扩展(本文以版本v2022.4.1 为例)之后,默认的调试工具在默认状态下即已可以进行多进程和多线程调试。

此处所谓的默认调试工具是指 python 扩展中的 debugpy;默认状态是指不采用 launch.json 进行额外的配置, 而是采用默认按钮点击的方式(当然,并不推荐采用这个中默认形式,应为其灵活性不足,只是说其以满足要求)。

如下面的简单例子。开了两个进程,一个进程死循环打印,另一个进程里开了一个线程打印主线程死循环加。

rom threading import Thread
from multiprocessing import Process
import time


def print_num(num):
    while 1:
        time.sleep(1)
        print(num)


def multithread_func():
    num = 2
    a = [num, ]
    thread = Thread(target=print_num, args=(a, ))
    thread.start()
    while 1:
        time.sleep(1)
        a[0] = a[0] + 1


def another_process(num):
    while 1:
        time.sleep(1)
        print(f"I'm the second, num is {num}.")


if __name__ == "__main__":
    from multiprocessing import Process
    process = [Process(target=multithread_func),
               Process(target=another_process, args=(2,)), ]
    [p.start() for p in process]
    [p.join() for p in process]

以如下方式(三个 time.sleep(1) 处)加入断点,点击相应的 Debug Python File,则在左侧可以找到对应的进程和线程的控制按钮。

带参数调试

对于带参数调试,则需要配置相应的 launch.json, 这个可以参见 vscode 官方的例子,此外其实还可以添加很多其他的选项,包括控制脚本的启动方式等。

最后提一下 justMyCode 选项, vscode 生成的默认 当前文件调试配置 是默认将该选项设置为 true 的。所以无法调试工程目录之外的文件,如环境中包含的库文件。可在 lauch.json 中将其设置为 false.

注意:启动 launch.json 配置的按钮在调试工具栏中,而不是原来的默认按钮

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/787018.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-05
下一篇2022-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存