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.:
In your code, when parsing the “commandline”,
tasksetis seeing the string-c2which according to many commandline parsing libraries is the same thing as-c -2which would explain the error that you’re seeing.Sometimes these things are easier to read if you use string interpolation instead:
Or new style
.format:And finally, no solution using
os.systemshould go by without at least a mention of the pythonsubprocessmodule.It’ll avoid the shell altogether which is slightly more efficient and makes you safer from shell injection types of attacks.