首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >任务集- python

任务集- python
EN

Stack Overflow用户
提问于 2013-02-06 04:51:25
回答 3查看 6K关注 0票数 3

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

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

代码语言:javascript
复制
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)

上面写着:

代码语言:javascript
复制
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运行同样的东西。

代码语言:javascript
复制
taskset -c 2 python <path>/run-apps.py lbm &

不知道问题出在哪里..

有什么提示吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-06 04:53:22

与你发布的命令行相比,你少了几个空格...例如:

代码语言:javascript
复制
cmd = "taskset -c " +  str(mapping[r]) + " python <path>/run-apps.py " + thr[r] + " &"

在您的代码中,当解析“命令行”时,taskset看到字符串-c2,根据许多命令行解析库,它与-c -2是相同的,这可以解释您看到的错误。

有时,如果使用字符串插值,这些内容会更容易阅读:

代码语言:javascript
复制
cmd = "taskset -c %s python <path>/run-apps.py %s &" % (mapping[r],thr[r])

或新风格的.format

代码语言:javascript
复制
cmd = "taskset -c {0} python <path>/run-apps.py {1} &".format(mapping[r],thr[r])

最后,任何使用os.system的解决方案都应该至少提到python subprocess模块。

代码语言:javascript
复制
process = subprocess.Popen(['taskset',
                            '-c',
                            str(mapping[r]),
                            'python',
                            '<path>/run-apps.py',
                            str(thr[r]) ] )

它将完全避免shell,这稍微更有效率,并使您更安全地免受shell注入类型的攻击。

票数 3
EN

Stack Overflow用户

发布于 2016-11-29 09:43:57

您可以避免调用taskset,而使用psutil:https://pythonhosted.org/psutil/#psutil.Process.cpu_affinity

代码语言:javascript
复制
>>> 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)
>>>
票数 5
EN

Stack Overflow用户

发布于 2013-02-06 05:01:33

您缺少空格。为了帮助自己,请尝试以下操作:

  1. 注释掉操作系统。system()行
  2. 操作系统下方。system()行,添加: 打印cmd

您将看到您的命令行是什么样子的。在需要的地方添加空格。然后取消对os.system()行的注释。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14716659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档