如何在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
相关产品推荐
相关产品推荐

