You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VSCode调试.json文件中添加taskset配置?

在VS Code调试配置中用taskset限制CPU核心列表的正确方法

要实现终端中taskset -c 13-25 python run.py --dir out/的CPU绑定效果,直接通过env设置TASKSET或GOMP_CPU_AFFINITY通常不生效(后者仅针对OpenMP程序),下面是两种可行的配置方式:

方式一:直接在launch.json中指定taskset作为启动命令

修改.vscode/launch.json,将command设为taskset,并按终端执行顺序传递所有参数:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 绑定CPU核心",
            "type": "python",
            "request": "launch",
            "command": "taskset",
            "args": [
                "-c", "13-25",
                "python", "${workspaceFolder}/run.py", "--dir", "out/"
            ],
            "console": "integratedTerminal"
        }
    ]
}

注意args的顺序必须和终端执行的完全一致:先传taskset的参数,再传python命令和脚本参数。

方式二:通过preLaunchTask绑定CPU

先在.vscode/tasks.json中定义CPU绑定任务:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "绑定CPU核心",
            "type": "shell",
            "command": "taskset -c 13-25 python ${workspaceFolder}/run.py --dir out/",
            "isBackground": false,
            "problemMatcher": []
        }
    ]
}

然后在launch.json中引用该前置任务:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 调试(CPU绑定)",
            "type": "python",
            "request": "launch",
            "preLaunchTask": "绑定CPU核心",
            "program": "${workspaceFolder}/run.py",
            "args": ["--dir", "out/"],
            "console": "integratedTerminal"
        }
    ]
}

常见问题说明

  • GOMP_CPU_AFFINITY仅对使用OpenMP并行的程序生效,无法全局绑定整个Python进程的CPU。
  • 直接设置command: "taskset"时,必须确保args参数顺序正确,否则会出现命令执行错误。

内容的提问来源于stack exchange,提问作者Glory Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 23:15:14