简体   繁体   中英

TypeError: int() argument must be a string or a number, not 'Popen' Python

I have these lines using Popen

numcpu = sub.Popen("($(cat /proc/cpuinfo | grep 'physical id' | awk '{print $NF}' | sort | uniq | wc -l))", shell=True, stdout=sub.PIPE, stderr=sub.PIPE)
numcores = sub.Popen("($(cat /proc/cpuinfo | grep 'cpu cores' | awk '{print $NF}' | sort | uniq | wc -l))", shell=True, stdout=sub.PIPE, stderr=sub.PIPE)  
numsibling = sub.Popen("($(cat /proc/cpuinfo | grep 'siblings' | awk '{print $NF}' | sort | uniq | wc -l))", shell=True, stdout=sub.PIPE, stderr=sub.PIPE)

but run into this traceback error

Traceback (most recent call last):
File "./cputool", line 53, in <module>
CpuTool()
File "./cputool", line 45, in CpuTool
numthreads = int(numsibling)/int(numcores)
TypeError: int() argument must be a string or a number, not 'Popen'

On this line

numthreads = int(numsibling)/int(numcores)

I'm new to scripting is there anyway to assign these Popen values to a string or int so this works? I don't think doing a simple numcpu== 'anything' works but I could be wrong? Any help for a beginner? Nothing I can find on here (SO) for similar cases, work for my case unfortunately. Thanks!

您可以使用proc_name.stdout.readline()读取标准输出,然后将其转换为int

numthreads = int(numsibling.stdout.readline())/int(numcores.stdout.readline())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM