Python论坛  - 讨论区

标题:[python-chinese] 如何获得当前系统CPU占用率和内存使用情况?

2004年09月04日 星期六 06:09

Qiangning Hong hongqn at gmail.com
Sat Sep 4 06:09:59 HKT 2004

看了一下wxPython、Python和Pythonwin的文档,没找到该用什么

求助,谢了

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年09月04日 星期六 08:32

limodou limodou at gmail.com
Sat Sep 4 08:32:50 HKT 2004

在csdn上有一个贴子,是使用win32api函数。

http://community.csdn.net/Expert/topic/3292/3292666.xml?temp=.4743006

On Sat, 4 Sep 2004 06:09:59 +0800, Qiangning Hong <hongqn at gmail.com> wrote:
> 看了一下wxPython、Python和Pythonwin的文档,没找到该用什么
> 
> 求助,谢了
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 



-- 
I like python!


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年09月05日 星期日 23:22

Qiangning Hong hongqn at gmail.com
Sun Sep 5 23:22:52 HKT 2004

On Sat, 4 Sep 2004 08:32:50 +0800, limodou <limodou at gmail.com> wrote:
> 在csdn上有一个贴子,是使用win32api函数。
> 
> http://community.csdn.net/Expert/topic/3292/3292666.xml?temp=.4743006
> 
> On Sat, 4 Sep 2004 06:09:59 +0800, Qiangning Hong <hongqn at gmail.com> wrote:
> > 看了一下wxPython、Python和Pythonwin的文档,没找到该用什么
> >
> > 求助,谢了
> >
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> 
> --
> I like python!
>
-------------- next part --------------
# perfmon, a python library to get the system performance infomation via
# win32pdh

# Copyright (C) 2004  Qiangning Hong <hongqn at gmail.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Monitor CPU Usage.

Supported Environment:
    - Windows 2000/XP
    
Prerequirements:
    - pywin32 (was win32all)
"""

import win32pdh

class PerfInfo:
    def __init__(self, counter_path):
        self.path = counter_path
        if win32pdh.ValidatePath(self.path) != 0:
            raise Exception('Invalid path: %s' % counter_path)
        self.query = self.counter = None

    def __del__(self):
        self.stop()

    def start(self):
        try:
            self.query = win32pdh.OpenQuery(None, 0)
            self.counter = win32pdh.AddCounter(self.query, self.path, 0)
        except:
            self.stop()
            raise

    def stop(self):
        if self.counter is not None:
            win32pdh.RemoveCounter(self.counter)
        if self.query is not None:
            win32pdh.CloseQuery(self.query)
        self.counter = self.query = None

    def get_value(self):
        win32pdh.CollectQueryData(self.query)
        status, value = win32pdh.GetFormattedCounterValue(self.counter,
                win32pdh.PDH_FMT_LONG)
        return value
            

class CPUUsage(PerfInfo):
    def __init__(self):
        PerfInfo.__init__(self, r'\Processor(_Total)\% Processor Time')

    def start(self):
        # Ignore the first collected data because it always is 99%
        PerfInfo.start(self)
        self.get_value()


def _test():
    import time

    cpu_usage = CPUUsage()
    cpu_usage.start()

    try:
        while True:
            percent = cpu_usage.get_value()
            print 'CPU Usage: %d%% (Press Ctrl-C to stop)' % percent
            time.sleep(1)
    except KeyboardInterrupt:
        pass
        
    cpu_usage.stop()

if __name__ == '__main__':
    _test()
        

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年09月05日 星期日 23:23

Qiangning Hong hongqn at gmail.com
Sun Sep 5 23:23:29 HKT 2004

你给的链接一直无法连接,后来我google了一下,找到一个使用Perfermence Data Helper的例子
http://www.codeproject.com/atl/desktop_perf_mon.asp

照着样子自己用win32all的win32pdh包装了一个模块,放在附件里了,可以监视CPU使用情况的。非常简陋,不过大家感兴趣的话可以对它进行扩充。

"""Monitor CPU Usage.

Supported Environment:
   - Windows 2000/XP

Prerequirements:
   - pywin32 (was win32all)
"""

from perfmon import CPUUsage

def _test():
   import time

   cpu_usage = CPUUsage()
   cpu_usage.start()

   try:
       while True:
           percent = cpu_usage.get_value()
           print 'CPU Usage: %d%% (Press Ctrl-C to stop)' % percent
           time.sleep(1)
   except KeyboardInterrupt:
       pass

   cpu_usage.stop()


On Sat, 4 Sep 2004 08:32:50 +0800, limodou <limodou at gmail.com> wrote:
> 在csdn上有一个贴子,是使用win32api函数。
> 
> http://community.csdn.net/Expert/topic/3292/3292666.xml?temp=.4743006
> 
> On Sat, 4 Sep 2004 06:09:59 +0800, Qiangning Hong <hongqn at gmail.com> wrote:
> > 看了一下wxPython、Python和Pythonwin的文档,没找到该用什么
> >
> > 求助,谢了
> >
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> 
> --
> I like python!
>
-------------- next part --------------
# perfmon, a python library to get the system performance infomation via
# win32pdh

# Copyright (C) 2004  Qiangning Hong <hongqn at gmail.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Monitor CPU Usage.

Supported Environment:
    - Windows 2000/XP
    
Prerequirements:
    - pywin32 (was win32all)
"""

import win32pdh

class PerfInfo:
    def __init__(self, counter_path):
        self.path = counter_path
        if win32pdh.ValidatePath(self.path) != 0:
            raise Exception('Invalid path: %s' % counter_path)
        self.query = self.counter = None

    def __del__(self):
        self.stop()

    def start(self):
        try:
            self.query = win32pdh.OpenQuery(None, 0)
            self.counter = win32pdh.AddCounter(self.query, self.path, 0)
        except:
            self.stop()
            raise

    def stop(self):
        if self.counter is not None:
            win32pdh.RemoveCounter(self.counter)
        if self.query is not None:
            win32pdh.CloseQuery(self.query)
        self.counter = self.query = None

    def get_value(self):
        win32pdh.CollectQueryData(self.query)
        status, value = win32pdh.GetFormattedCounterValue(self.counter,
                win32pdh.PDH_FMT_LONG)
        return value
            

class CPUUsage(PerfInfo):
    def __init__(self):
        PerfInfo.__init__(self, r'\Processor(_Total)\% Processor Time')

    def start(self):
        # Ignore the first collected data because it always is 99%
        PerfInfo.start(self)
        self.get_value()


def _test():
    import time

    cpu_usage = CPUUsage()
    cpu_usage.start()

    try:
        while True:
            percent = cpu_usage.get_value()
            print 'CPU Usage: %d%% (Press Ctrl-C to stop)' % percent
            time.sleep(1)
    except KeyboardInterrupt:
        pass
        
    cpu_usage.stop()

if __name__ == '__main__':
    _test()
        

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年09月06日 星期一 08:54

limodou limodou at gmail.com
Mon Sep 6 08:54:21 HKT 2004

我可以登录啊,这是上面的一个人给出的用win32api调用windows api得到相应的结果:

import win32wnet
import win32api

print win32wnet.WNetGetUser()  # 取机本名

print win32api.GetSystemInfo()

(0, 4096, 65536, 2147418111, 1, 1, 586, 65536, (15, 521))

上面GetSystemInfo

返回值类于
typedef struct _SYSTEM_INFO { 
  union { 
    DWORD  dwOemId; 
    struct { 
      WORD wProcessorArchitecture; 
      WORD wReserved; 
    }; 
  }; 
  DWORD  dwPageSize; 
  LPVOID lpMinimumApplicationAddress; 
  LPVOID lpMaximumApplicationAddress; 
  DWORD_PTR dwActiveProcessorMask; 
  DWORD dwNumberOfProcessors; 
  DWORD dwProcessorType; 
  DWORD dwAllocationGranularity; 
  WORD wProcessorLevel; 
  WORD wProcessorRevision; 
} SYSTEM_INFO;



On Sun, 5 Sep 2004 23:23:29 +0800, Qiangning Hong <hongqn at gmail.com> wrote:
> 
> 
> 你给的链接一直无法连接,后来我google了一下,找到一个使用Perfermence Data Helper的例子
> http://www.codeproject.com/atl/desktop_perf_mon.asp
> 
> 照着样子自己用win32all的win32pdh包装了一个模块,放在附件里了,可以监视CPU使用情况的。非常简陋,不过大家感兴趣的话可以对它进行扩充。
> 
> """Monitor CPU Usage.
> 
> Supported Environment:
>   - Windows 2000/XP
> 
> Prerequirements:
>   - pywin32 (was win32all)
> """
> 
> from perfmon import CPUUsage
> 
> def _test():
>   import time
> 
>   cpu_usage = CPUUsage()
>   cpu_usage.start()
> 
>   try:
>       while True:
>           percent = cpu_usage.get_value()
>           print 'CPU Usage: %d%% (Press Ctrl-C to stop)' % percent
>           time.sleep(1)
>   except KeyboardInterrupt:
>       pass
> 
>   cpu_usage.stop()
> 
> On Sat, 4 Sep 2004 08:32:50 +0800, limodou <limodou at gmail.com> wrote:
> > 在csdn上有一个贴子,是使用win32api函数。
> >
> > http://community.csdn.net/Expert/topic/3292/3292666.xml?temp=.4743006
> >
> > On Sat, 4 Sep 2004 06:09:59 +0800, Qiangning Hong <hongqn at gmail.com> wrote:
> > > 看了一下wxPython、Python和Pythonwin的文档,没找到该用什么
> > >
> > > 求助,谢了
> > >
> > >
> > > _______________________________________________
> > > python-chinese list
> > > python-chinese at lists.python.cn
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> >
> > --
> > I like python!
> >
> 
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 
> 
> 



-- 
I like python!


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号