我有一台双四核的机器。所以,我的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是可用的,我将从commandline运行同样的东西。
taskset -c 2 python <path>/run-apps.py lbm &不知道问题出在哪里..
有什么提示吗?
发布于 2013-02-06 04:53:22
与你发布的命令行相比,你少了几个空格...例如:
cmd = "taskset -c " + str(mapping[r]) + " python <path>/run-apps.py " + thr[r] + " &"在您的代码中,当解析“命令行”时,taskset看到字符串-c2,根据许多命令行解析库,它与-c -2是相同的,这可以解释您看到的错误。
有时,如果使用字符串插值,这些内容会更容易阅读:
cmd = "taskset -c %s python <path>/run-apps.py %s &" % (mapping[r],thr[r])或新风格的.format
cmd = "taskset -c {0} python <path>/run-apps.py {1} &".format(mapping[r],thr[r])最后,任何使用os.system的解决方案都应该至少提到python subprocess模块。
process = subprocess.Popen(['taskset',
'-c',
str(mapping[r]),
'python',
'<path>/run-apps.py',
str(thr[r]) ] )它将完全避免shell,这稍微更有效率,并使您更安全地免受shell注入类型的攻击。
发布于 2016-11-29 09:43:57
您可以避免调用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)
>>>发布于 2013-02-06 05:01:33
您缺少空格。为了帮助自己,请尝试以下操作:
您将看到您的命令行是什么样子的。在需要的地方添加空格。然后取消对os.system()行的注释。
https://stackoverflow.com/questions/14716659
复制相似问题