当前位置: 首页>>代码示例>>Python>>正文


Python Process.get_cpu_percent方法代码示例

本文整理汇总了Python中psutil.Process.get_cpu_percent方法的典型用法代码示例。如果您正苦于以下问题:Python Process.get_cpu_percent方法的具体用法?Python Process.get_cpu_percent怎么用?Python Process.get_cpu_percent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在psutil.Process的用法示例。


在下文中一共展示了Process.get_cpu_percent方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_percent [as 别名]
def main(argv):
    p = Popen(argv[1:])
    
    try:
        client = ExitCodeClient()
        try:
            proc = Process(pid=p.pid)
            while p.poll() is None:
                total_cpu_percent = proc.get_cpu_percent(interval=0)
                for child_proc in proc.get_children(recursive=True):
                    total_cpu_percent += child_proc.get_cpu_percent(interval=0)
                client.send_status(total_cpu_percent)
                time.sleep(0.1) # recommended waiting period from psutil docs
        except NoSuchProcess:
            pass
    except KeyboardInterrupt:
        try:
            p.terminate()
        except OSError:
            pass
    
    client.send_status(LEDMode.Success if p.returncode == 0 else LEDMode.Error)
    time.sleep(0.5) # Give server time to read value before connection closes
    client.shutdown()
    
    return p.returncode
开发者ID:logiconcepts819,项目名称:program-status-server,代码行数:28,代码来源:main.py

示例2: run

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_percent [as 别名]
 def run(self):
     self._isRunning = True
     process = Process(os.getpid())
     while self._isRunning:
         self.cpuUsage = process.get_cpu_percent(self._pollInterval)
         self.memUsage = process.get_memory_info()[0]
         if self.delegate is not None:
             self.delegate.processCpuUsage(self.cpuUsage)
             self.delegate.processMemUsage(self.memUsage)
开发者ID:teragonaudio,项目名称:Lucidity,代码行数:11,代码来源:performance.py

示例3: update_process_list

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_percent [as 别名]
def update_process_list():
    global PS
    plist = []
    for pid in psutil.get_pid_list():
        try:
            p = Process(pid)
            plist.append((p,p.get_cpu_percent(),p.get_memory_percent())) 
        except psutil.NoSuchProcess:
            continue
    # copy plist to PS
    PS = sorted(plist[:], key=lambda e: e[1] if e[1] > e[2] else e[2], reverse=True)[:25]
开发者ID:ViktorNova,项目名称:mod-ui,代码行数:13,代码来源:sysmond.py

示例4: __init__

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_percent [as 别名]
class Tracer:
    def __init__(self, pid):
        self.process = Process(pid)
        self.gdb = Gdb(pid)
        self.io_read_bytes = 0
        self.io_write_bytes = 0
        self.about = (self.process.name, pid, datetime.now())

    def snapshot(self):
        cpu = self.process.get_cpu_percent()
        mem = self.process.get_memory_info()[0] # only RSS for now
        io = self.get_io_bytes()
        stack = self.gdb.get_stack()
        return (cpu, mem, io, stack)

    def get_io_bytes(self):
        _, _, read_bytes, write_bytes = self.process.get_io_counters()
        io = (read_bytes - self.io_read_bytes, write_bytes - self.io_write_bytes)
        self.io_read_bytes, self.io_write_bytes = read_bytes, write_bytes
        return io
开发者ID:yieldthought,项目名称:codechart,代码行数:22,代码来源:tracer.py

示例5: get_info

# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_percent [as 别名]
def get_info(process=None, interval=0):
    """Return information about a process.

    If process is None, will return the information about the current process
    """
    if process is None:
        process = Process(os.getpid())

    info = {}
    try:
        mem_info = process.get_memory_info()
        info['mem_info1'] = bytes2human(mem_info[0])
        info['mem_info2'] = bytes2human(mem_info[1])
    except AccessDenied:
        info['mem_info1'] = info['mem_info2'] = "N/A"

    try:
        info['cpu'] = process.get_cpu_percent(interval=interval)
    except AccessDenied:
        info['cpu'] = "N/A"

    try:
        info['mem'] = round(process.get_memory_percent(), 1)
    except AccessDenied:
        info['mem'] = "N/A"

    try:
        cpu_times = process.get_cpu_times()
        ctime = timedelta(seconds=sum(cpu_times))
        ctime = "%s:%s.%s" % (ctime.seconds // 60 % 60,
                        str((ctime.seconds % 60)).zfill(2),
                        str(ctime.microseconds)[:2])
    except AccessDenied:
        ctime = "N/A"

    info['ctime'] = ctime

    try:
        info['pid'] = process.pid
    except AccessDenied:
        info['pid'] = 'N/A'

    try:
        info['username'] = process.username
    except AccessDenied:
        info['username'] = 'N/A'

    try:
        info['nice'] = process.nice
    except AccessDenied:
        info['nice'] = 'N/A'
    except NoSuchProcess:
        info['nice'] = 'Zombie'

    try:
        cmdline = os.path.basename(shlex.split(process.cmdline[0])[0])
    except (AccessDenied, IndexError):
        cmdline = "N/A"

    info['cmdline'] = cmdline

    info['children'] = []
    for child in process.get_children():
        info['children'].append(get_info(child, interval=interval))

    return info
开发者ID:kuzmich,项目名称:circus,代码行数:68,代码来源:util.py


注:本文中的psutil.Process.get_cpu_percent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。