gpt4 book ai didi

python - 任务集-python

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:09 26 4
gpt4 key购买 nike

我有一台双四核机器。所以,我的 cpu 列表是 0-7。

我正在尝试从 python 运行任务集

mapping = [2,2,2,2,2]
for i in range(0,len(mapping)):
cmd = "taskset -c" + str(mapping[r]) + "python <path>/run-apps.py" + thr[r] + "&"
os.system(cmd)

它说:

taskset: invalid option -- '2'
taskset (util-linux-ng 2.17.2)
usage: taskset [options] [mask | cpu-list] [pid | cmd [args...]]
set or get the affinity of a process

-p, --pid operate on existing given pid
-c, --cpu-list display and specify cpus in list format
-h, --help display this help
-V, --version output version information

The default behavior is to run a new command:
taskset 03 sshd -b 1024
You can retrieve the mask of an existing task:
taskset -p 700
Or set it:
taskset -p 03 700
List format uses a comma-separated list instead of a mask:
taskset -pc 0,3,7-11 700
Ranges in list format can take a stride argument:
e.g. 0-31:2 is equivalent to mask 0x55555555

但是核心 2 可用,我将从命令行运行同样的东西。

taskset -c 2 python <path>/run-apps.py lbm &

不知道是什么问题..

有什么提示吗?

最佳答案

您可以避免调用 taskset 并改用 psutil: https://pythonhosted.org/psutil/#psutil.Process.cpu_affinity

>>> import psutil
>>> psutil.cpu_count()
4
>>> p = psutil.Process()
>>> p.cpu_affinity() # get
[0, 1, 2, 3]
>>> p.cpu_affinity([0]) # set; from now on, process will run on CPU #0 only
>>> p.cpu_affinity()
[0]
>>>
>>> # reset affinity against all CPUs
>>> all_cpus = list(range(psutil.cpu_count()))
>>> p.cpu_affinity(all_cpus)
>>>

关于python - 任务集-python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com