在计算机上查找处理器数量的跨平台方式? [英] Cross platform way of finding number of processors on a machine?

查看:53
本文介绍了在计算机上查找处理器数量的跨平台方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法使用python代码(使用相同的代码在linux /

windows / macos / cygwin上)查找一台机器上的处理器数量/ cross

平台代码)?


Is there a way to find the number of processors on a machine (on linux/
windows/macos/cygwin) using python code (using the same code/cross
platform code)?

推荐答案

2007年10月4日,John< we **********@yahoo.comwrote:
On 10/4/07, John <we**********@yahoo.comwrote:

>

有没有办法找到的数量一台机器上的处理器(在linux /

windows / macos / cygwin上)使用python代码(使用相同的代码/交叉

平台代码)?
>
Is there a way to find the number of processors on a machine (on linux/
windows/macos/cygwin) using python code (using the same code/cross
platform code)?



没有一个电话可以在每个

平台上给你相同的信息,但你显然可以自己写这个在大多数情况下,在os.uname()[0]上基于开关



对于Darwin,你可以使用子进程模块调用''sysctl

hw.logicalcpu''。我不确定在python中是否有更直接的方式来使用sysctl。 (hw.logicalcpu_max是最大的硬件支持b
,但有人可能已经用OF阻止启动他们的机器

一些处理器你可能应该尊重这个决定)


对于Linux,您可以读取/ proc / cpuinfo并解析该信息。这个有点小心,但是,如果你的处理器支持HT,那么它们将显示为2,这可能是也可能不是你想要的。如果您知道哪个处理器系列真正是多核的,哪些是HT,那么
可以确定性地解析这些信息。


对于Win32,便宜又脏的方法是读取NUMBER_OF_PROCESSORS

环境变量。 99%的时间,这是正确的,所以

可能就足够了。在Windows上有一个系统调用

可以为你提供真实的虚拟数量
cpus,但我不知道它是什么。


我没有安装cygwin安装,但我的猜测是那里还有一个

sysctl选项。


一个注意事项:*所有*这些方法都会告诉您机器中虚拟的
CPU数量,而不是物理数量。几乎没有理由

为什么你关心这两个数字之间的区别,但是如果你做了b / b
,你将不得不竭尽全力在每个平台上探测实际的

硬件。 (并且在WinXP之前,Windows实际上并不知道
知道差异 - 所有处理器都被认为是物理的。)


此外,Darwin和Linux很容易让你获得

处理器的速度,但是在x86上这些数字不是C1E

和EIST(所有供应商的x86处理器都有能力)的最大值改变

速度取决于负载)。


-

尼克

There''s no single call that will give you the same info on every
platform, but you can obviously write this yourself and switch based
on os.uname()[0] in most cases.

For Darwin, you can just use the subprocess module to call ''sysctl
hw.logicalcpu''. I''m not sure if there''s a more direct way in python
to use sysctl. (hw.logicalcpu_max is what the hardware maximally
supports, but someone may have started their machine with OF blocking
some of the processors and you should probably respect that decision)

For Linux you can read /proc/cpuinfo and parse that information. Be
somewhat careful with this, however, if your processors support HT,
they will show as 2, and that may or may not be what you want. You
can deterministically parse this information out if you know which
processor families are truly multi-core, and which are HT.

For Win32, the cheap and dirty way is to read the NUMBER_OF_PROCESSORS
environment variable. 99% of the time, this will be correct, so it
might be sufficient for your purposes. There is a system call
available on windows that will net you the true number of virtual
cpus, but I don''t know what it is.

I don''t have a cygwin install laying around, but my guess is there''s a
sysctl option there too.

One note: *all* of these methods will tell you the virtual number of
CPUs in a machine, not the physical number. There''s almost no reason
why you care about the distinction between these two numbers, but if
you do, you''ll have to go to great lengths to probe the actual
hardware on each platform. (And pre-WinXP, Windows doesn''t actually
know the difference - all processors are presumed to be physical).

Also, Darwin and Linux will easily allow you to get the speed of the
processors, but on x86 these numbers are not the maximums due to C1E
and EIST (x86 processors from all vendors are capable of changing
speeds depending on load).

--
Nick

Nicholas Bastin写道:
Nicholas Bastin wrote:

2007年10月4日,John< we ********** @ yahoo.comwrote:
On 10/4/07, John <we**********@yahoo.comwrote:

>有没有办法使用python代码查找机器上的处理器数量(在linux /
windows / macos / cygwin上)(使用相同的代码/交叉平台代码)?
>Is there a way to find the number of processors on a machine (on linux/
windows/macos/cygwin) using python code (using the same code/cross
platform code)?



没有一个电话可以在每个

平台上提供相同的信息,但你显然可以自己写这个在大多数情况下,在os.uname()[0]上基于开关



There''s no single call that will give you the same info on every
platform, but you can obviously write this yourself and switch based
on os.uname()[0] in most cases.



关于没有获得一个跨平台的第二点

答案。在Windows下,WMI通常是这些东西的方式:

http://msdn2.microsoft.com/en-us/library/aa394373.aspx


但是,正如Nicholas所说:


"""

Windows Server 2003,Windows XP和Windows 2000:

这个属性不可用。

"""


因为它可能在任何早期都没有,

留给你Vista或早期采用的版本

下一个Windows Server产品。


TJG

Second that point about not getting one cross-platform
answer. Under Windows, WMI is often the way to go for
these things:

http://msdn2.microsoft.com/en-us/library/aa394373.aspx

but, as Nicholas noted:

"""
Windows Server 2003, Windows XP, and Windows 2000:
This property is not available.
"""

since it''s presumably not available in anything earlier either,
that leaves you with Vista or the early-adopter editions of the
next Windows Server product.

TJG


On Okt。,09:58,Tim Golden< m ... @ timgolden.me.ukwrote:
On 5 Okt., 09:58, Tim Golden <m...@timgolden.me.ukwrote:

Nicholas Bastin写道:
Nicholas Bastin wrote:

2007年10月4日,John< weekender ... @ yahoo.comwrote:
On 10/4/07, John <weekender...@yahoo.comwrote:

是吧ea使用python代码(在linux /

windows / macos / cygwin上)查找处理器数量的方法(使用相同的代码/交叉

平台代码) ?
Is there a way to find the number of processors on a machine (on linux/
windows/macos/cygwin) using python code (using the same code/cross
platform code)?


没有一个电话会在每个

平台上提供相同的信息,但是在大多数情况下,你可以自己写这个并在os.uname()[0]上基于

切换。
There''s no single call that will give you the same info on every
platform, but you can obviously write this yourself and switch based
on os.uname()[0] in most cases.



关于没有获得一个跨平台的第二点

答案。在Windows下,WMI通常是这些东西的方式:

http://msdn2.microsoft.com/en-us/library/aa394373.aspx


但是,正如Nicholas所说:


"""

Windows Server 2003,Windows XP和Windows 2000:

这个属性不可用。

"""


因为它可能在任何早期都没有,

留给你Vista或早期采用的版本

下一个Windows Server产品。


TJG


Second that point about not getting one cross-platform
answer. Under Windows, WMI is often the way to go for
these things:

http://msdn2.microsoft.com/en-us/library/aa394373.aspx

but, as Nicholas noted:

"""
Windows Server 2003, Windows XP, and Windows 2000:
This property is not available.
"""

since it''s presumably not available in anything earlier either,
that leaves you with Vista or the early-adopter editions of the
next Windows Server product.

TJG



非常出色。我有一个两年前的Windows XP双核笔记本和

当我问Python时我得到了正确的答案:

Remarkable. I''ve a two years old Windows XP dual core notebook and
when I''m asking Python I get the correct answer:


>> import os
os.environ [''NUMBER_OF_PROCESSORS'']
>>import os
os.environ[''NUMBER_OF_PROCESSORS'']



2





2

However this feature might not be guaranteed by Microsoft for
arbitrary computers using their OS?

Kay


这篇关于在计算机上查找处理器数量的跨平台方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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