python如何调用全部cpu_系统信息 - 如何使用python查找CPU数量

系统信息 - 如何使用python查找CPU数量

我想知道使用Python的本地机器上的CPU数量。 当使用最佳扩展的仅用户空间程序调用时,结果应为user/real,作为time(1)的输出。

11个解决方案

650 votes

如果你有一个版本> = 2.6的python你可以简单地使用

import multiprocessing

multiprocessing.cpu_count()

[http://docs.python.org/library/multiprocessing.html#multiprocessing.cpu_count]

Nadia Alramli answered 2019-01-07T03:59:04Z

151 votes

如果您对当前进程可用的处理器数量感兴趣,则必须先检查cpuset。 否则(或者如果cpuset未被使用),multiprocessing.cpu_count()是Python 2.6及更高版本的方法。 以下方法回溯到旧版Python中的几种替代方法:

import os

import re

import subprocess

def available_cpu_count():

""" Number of available virtual or physical CPUs on this system, i.e.

user/real as output by time(1) when called with an optimally scaling

userspace-only program"""

# cpuset

# cpuset may restrict the number of *available* processors

try:

m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$',

open('/proc/self/status').read())

if m:

res = bin(int(m.group(1).replace(',', ''), 16)).count('1')

if res > 0:

return res

except IOError:

pass

# Python 2.6+

try:

import multiprocessing

return multiprocessing.cpu_count()

except (Imp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值