本文整理汇总了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
示例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)
示例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]
示例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
示例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