0

Guys I want to retrieve a process' affinity list by using subprocess.

try:
    pid = 1500
    x = subprocess.check_output(['taskset','-c','-p', str(pid)])
except Exception as inst:
    print inst

However, this results in the following output:

pid 1500's correct affinity list: 0-3

How do I obtain the 0-3 value as a string directly? There can be only integers or a value like 0-3. Basically I'm trying to get what's after colon ':'.

Thanks in advance.

1
  • Python or bash regex? Commented Dec 15, 2016 at 11:40

3 Answers 3

1

Just split output on : and use index 1 to get 0-3. Later, You can do anything You want with it. Something like this:

try:
    pid = 1500
    x = subprocess.check_output(['taskset','-c','-p', str(pid)])
    affinity_list = [affinity for affinity in x.split(':')[1].split('-')]
except Exception as inst:
    print inst

This will produce:

affinity_list = [0, 3]

If You want to interpret 0-3 as a range, You could do this:

affinity_list = range(affinity_list[0], affinity_list[-1] + 1)
Sign up to request clarification or add additional context in comments.

Comments

1

You may do something like print(str(inst).split(':')[1], which gets the string representation of inst, splits it into an array with the delemiter ":" and takes the second value of this array (in your example case, it would be 0-3)

Comments

1

Well, split() is perfect for your case, but if you want to use regex you could do something like:

>>> import re
>>> x = "pid 1500's correct affinity list: 0-3"
>>> y = re.search(':.*$',x).group()[1:]
>>> y
' 0-3'

But again you don't really need regex for this task.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.