Python论坛  - 讨论区

标题:[python-chinese] 什么lib可以得到cpu,内存和网络的使用率

2007年09月03日 星期一 11:06

边 江 borderj在gmail.com
星期一 九月 3 11:06:44 HKT 2007

     想用python写个得到cpu,内存和网络的使用率的小程序,主要在windows系统中,要是可以跨平台最好不过。

不知道python自带的有没有这个lib,要是没有能不能推荐个lib。

谢谢

-- 
Blog:    www.borderj.cn

  Border
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070903/a5a1f127/attachment.html 

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

2007年09月03日 星期一 13:37

lvhongqing2008 lvhongqing2008在sohu.com
星期一 九月 3 13:37:02 HKT 2007

Äá¡´«Í³ÓïÑÔдһ¸öÃüÁîÐеijÌÐò£¬È»ºóÓÃpopen¡¡¶Á.
È»ºó°ÑÊý¾Ý¿ÉÊÓµÄչʾ³öÀ´.

ÎÒ×öÏà¹ØµÄ¶«Î÷¡¡¾ÍÊÇÕâô¸ÉµÄ.




lvhongqing2008
2007-09-03



·¢¼þÈË£º ±ß ½­
·¢ËÍʱ¼ä£º 2007-09-03 11:07:05
ÊÕ¼þÈË£º python-chinese在lists.python.cn; python-cn在googlegroups.com
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese] ʲôlib¿ÉÒԵõ½cpu,ÄÚ´æºÍÍøÂçµÄʹÓÃÂÊ


     ÏëÓÃpythonд¸öµÃµ½cpu£¬ÄÚ´æºÍÍøÂçµÄʹÓÃÂʵÄС³ÌÐò£¬Ö÷ÒªÔÚwindowsϵͳÖУ¬ÒªÊÇ¿ÉÒÔ¿çÆ½Ì¨×îºÃ²»¹ý¡£

²»ÖªµÀpython×Ô´øµÄÓÐûÓÐÕâ¸ölib£¬ÒªÊÇûÓÐÄܲ»ÄÜÍÆ¼ö¸ölib¡£

лл

-- 
Blog:    www.borderj.cn

  Border 
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070903/0ce00446/attachment.htm 

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

2007年09月03日 星期一 13:54

边 江 borderj在gmail.com
星期一 九月 3 13:54:50 HKT 2007

To: Qiangning Hong

   从pywin32 <http://sourceforge.net/projects/pywin32>刚装上,你的程序调通了。

   在看文档,以后在遇到什么问题再请教。

谢谢

-- 
Blog:    www.borderj.cn

  Border
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070903/4604967d/attachment.htm 

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

2007年09月03日 星期一 14:14

边 江 borderj在gmail.com
星期一 九月 3 14:14:13 HKT 2007

从网上搜了个你以前写的:

# perfmon, a python library to get the system performance infomation via
# win32pdh

# Copyright (C) 2004  Qiangning Hong 

# 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()



-- 
Blog:    www.borderj.cn

  Border
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070903/1d3bd3c7/attachment-0001.htm 

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

2007年09月03日 星期一 14:17

beck917 beck917在gmail.com
星期一 九月 3 14:17:23 HKT 2007

 

ÔÎ

  _____  

·¢¼þÈË: python-chinese-bounces在lists.python.cn
[mailto:python-chinese-bounces在lists.python.cn] ´ú±í ±ß ½­
·¢ËÍʱ¼ä: 2007Äê9ÔÂ3ÈÕ 14:14
ÊÕ¼þÈË: python-chinese在lists.python.cn
Ö÷Ìâ: Re: [python-chinese]ʲôlib¿ÉÒԵõ½cpu,ÄÚ´æºÍÍøÂçµÄʹÓÃÂÊ

 


´ÓÍøÉÏËÑÁ˸öÄãÒÔǰдµÄ£º

# perfmon, a python library to get the system performance infomation via
# win32pdh

# Copyright (C) 2004  Qiangning Hong 

# 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()



-- 
Blog:    www.borderj.cn

  Border 

-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070903/7c3e7546/attachment.html 

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

2007年09月03日 星期一 15:36

Qiangning Hong hongqn在gmail.com
星期一 九月 3 15:36:07 HKT 2007

On 9/3/07, 边 江 <borderj在gmail.com> wrote:
> 从网上搜了个你以前写的:

嗯,今天发给你的是后来的进化版本。简化了程序结构,增加了内存占用信息。

-- 
Qiangning Hong
http://www.douban.com/people/hongqn/

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

2007年09月04日 星期二 13:46

doudou doudou array.doudou在gmail.com
星期二 九月 4 13:46:14 HKT 2007

我这里有个在win32下提取系统基本信息,包括CPU使用率/内存使用情况的Python扩展包,用C写的,附带二进制安装包。因为是公司开发的,如果想要需要收一定费用。
想要就私下联系我吧。

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号