本文整理汇总了Python中psutil.Process.get_cpu_times方法的典型用法代码示例。如果您正苦于以下问题:Python Process.get_cpu_times方法的具体用法?Python Process.get_cpu_times怎么用?Python Process.get_cpu_times使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Process
的用法示例。
在下文中一共展示了Process.get_cpu_times方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_time_used_by_tree
# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_times [as 别名]
def get_time_used_by_tree(proc = this_proc):
"""
Gets the CPU time used by a process and all its children (user+sys). If the process is not
provided, this process is used. The argument must be a pid or a psutils.Process object.
Return values is in seconds.
"""
if isinstance(proc, int): proc = Process(proc) # was given a PID
time = sum(proc.get_cpu_times())
for p in proc.get_children(True):
try:
if p.is_running():
time += sum(p.get_cpu_times())
except: pass
return time
示例2: get_info
# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import get_cpu_times [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