任务集-python

pis*_*tal 3 python linux

我有一台双四核机器。所以,我的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)
Run Code Online (Sandbox Code Playgroud)

它说:

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
Run Code Online (Sandbox Code Playgroud)

但 core 2 可用,我将从 运行同样的事情commandline

taskset -c 2 python <path>/run-apps.py lbm &
Run Code Online (Sandbox Code Playgroud)

不知道问题是什么..

有什么提示吗?

Gia*_*olà 8

您可以避免调用任务集并使用 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)
>>>
Run Code Online (Sandbox Code Playgroud)