如何在vscode中同时运行多个文件——server/client模式——在launch.json文件中设置多个configurations再compounds

在vscode中我们一般都是同一时间只运行一个代码,但是这种设置并不适合server/client模式,甚至有很多分布式和并行的项目需要同一时间运行多个client,针对这种情况我们可以通过设置vscode中的launch.json文件来实现。

 

 

我们可以在launch.json文件中设置多个configurations再compounds,以此实现同时运行多个程序,这里以python项目为例,给出launch.json文件内容:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Server Python",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/server.py",
            "console": "integratedTerminal",
            "justMyCode": true,

            "python": "/home/devil/PycharmProjects/py/bin/python",
            "cwd": "${workspaceFolder}",
            "env": {"PYTHONPATH":"${workspaceFolder}"}
        },

        {
            "name": "Client Python_0",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/client.py",
            "console": "integratedTerminal",
            "justMyCode": true,

            "python": "/home/devil/PycharmProjects/py/bin/python",
            "cwd": "${workspaceFolder}",
            "env": {"PYTHONPATH":"${workspaceFolder}"}
        },

        {
            "name": "Client Python_1",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/client.py",
            "console": "integratedTerminal",
            "justMyCode": true,

            "python": "/home/devil/PycharmProjects/py/bin/python",
            "cwd": "${workspaceFolder}",
            "env": {"PYTHONPATH":"${workspaceFolder}"},
        },
    ],

    "compounds": [
        {
            "name": "Compound",
            "configurations": ["Server Python", "Client Python_0", "Client Python_1"],
            "presentation": {
                "hidden": false,
                "group": "",
                "order": 1
            }
        }
    ]

}

 

 

 

可以看到上面分布做了四个配置,名字为:"Server Python""Client Python_0""Client Python_1", "Compound",其中"Compound"配置是一个混合配置,该配置的执行等于同时并行执行配置"Server Python", "Client Python_0", "Client Python_1"

 

 

在vscode的调试窗口中可以看到这个四个配置:

 

 

 

 

 

 

 

 

 

按键F5,因为"Compound"执行顺序为1因此执行"Compound"配置,但是实际上执行的是"Server Python", "Client Python_0", "Client Python_1"这三个配置:

 

 

 

 

 

 

 

 

 

 

 

 

 

=================================================

 

posted on 2022-06-30 10:23  Angry_Panda  阅读(4251)  评论(0编辑  收藏  举报

导航