任务集-python [英] taskset - python

查看:35
本文介绍了任务集-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双四核计算机.因此,我的CPU列表为0-7.

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

 映射= [2,2,2,2,2]对于范围(0,len(mapping))中的i:cmd ="taskset -c" + str(mapping [r])+"python< path>/run-apps.py" + thr [r] +&"操作系统.(cmd) 

它说:

  taskset:无效选项-'2'任务集(util-linux-ng 2.17.2)用法:任务集[选项] [掩码|cpu-list] [pid |cmd [args ...]设置或获取流程的亲和力-p,--pid对现有的给定pid进行操作-c,-cpu-list以列表格式显示并指定cpus-h,--help显示此帮助-V,--version输出版本信息默认行为是运行新命令:任务集03 sshd -b 1024您可以检索现有任务的掩码:任务集-p 700或设置它:任务集-p 03700列表格式使用逗号分隔的列表而不是掩码:任务集-pc 0,3,7-11 700列表格式的范围可以采用跨步参数:例如0-31:2等效于掩码0x55555555 

但是核心 2 可用,我要从 commandline 运行相同的东西.

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

不知道问题出在什么地方.

有什么提示吗?

解决方案

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

  cmd ="taskset -c" + str(mapping [r])+"python< path>/run-apps.py" + thr [r] +&" 

在您的代码中,当解析命令行"时, taskset 看到了字符串 -c2 ,根据许多命令行解析库,该字符串与相同-c -2 会解释您所看到的错误.

有时候,如果使用字符串插值代替,这些内容将更易于阅读:

  cmd =任务集-c%s python< path>/run-apps.py%s&"%(mapping [r],thr [r]) 

或新样式 .format :

  cmd =任务集-c {0} python< path>/run-apps.py {1}&".format(mapping [r],thr [r]) 

最后,在没有至少提及python subprocess 模块的情况下,使用 os.system 的任何解决方案都不会通过.

process = subprocess.Popen(['taskset','-C',str(mapping [r]),'Python','< path>/run-apps.py',str(thr [r])]) 

它将完全避免使用shell,因为shell效率更高,并且可以免受shell注入类型的攻击.<​​/p>

I have a dual quad core machine. so, the cpu list for me 0-7.

I am trying to run the taskset from 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)

and it says:

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

But the core 2 is available and I am to run the same thing from commandline.

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

not a clue what the issue is..

any hints?

解决方案

compared to the commandline you posted, you're missing a couple spaces ... e.g.:

cmd = "taskset -c " +  str(mapping[r]) + " python <path>/run-apps.py " + thr[r] + " &"

In your code, when parsing the "commandline", taskset is seeing the string -c2 which according to many commandline parsing libraries is the same thing as -c -2 which would explain the error that you're seeing.

Sometimes these things are easier to read if you use string interpolation instead:

cmd = "taskset -c %s python <path>/run-apps.py %s &" % (mapping[r],thr[r])

Or new style .format:

cmd = "taskset -c {0} python <path>/run-apps.py {1} &".format(mapping[r],thr[r])

And finally, no solution using os.system should go by without at least a mention of the python subprocess module.

process = subprocess.Popen(['taskset',
                            '-c',
                            str(mapping[r]),
                            'python',
                            '<path>/run-apps.py',
                            str(thr[r]) ] )

It'll avoid the shell altogether which is slightly more efficient and makes you safer from shell injection types of attacks.

这篇关于任务集-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆