本文整理汇总了Python中psutil.cpu_times方法的典型用法代码示例。如果您正苦于以下问题:Python psutil.cpu_times方法的具体用法?Python psutil.cpu_times怎么用?Python psutil.cpu_times使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil
的用法示例。
在下文中一共展示了psutil.cpu_times方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getSystemInfo
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def getSystemInfo(cls):
system = {
'boot_time': psutil.boot_time(),
'cpu_count': psutil.cpu_count(),
'cpu_stats': psutil.cpu_stats().__dict__,
'cpu_times': [k.__dict__ for k in psutil.cpu_times(percpu=True)],
'disk_io_counters': psutil.disk_io_counters().__dict__,
'disk_usage': [],
'net_io_counters': psutil.net_io_counters().__dict__,
'swap_memory': psutil.swap_memory().__dict__,
'virtual_memory': psutil.virtual_memory().__dict__
}
partitions = psutil.disk_partitions()
for p in partitions:
if p.mountpoint in cls.INCLUDED_PARTITIONS:
usage = psutil.disk_usage(p.mountpoint)
system['disk_usage'].append({
'mountpoint': p.mountpoint,
'total': usage.total,
'used': usage.used
})
return system
示例2: test_serialization
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_serialization(self):
def check(ret):
if json is not None:
json.loads(json.dumps(ret))
a = pickle.dumps(ret)
b = pickle.loads(a)
self.assertEqual(ret, b)
check(psutil.Process().as_dict())
check(psutil.virtual_memory())
check(psutil.swap_memory())
check(psutil.cpu_times())
check(psutil.cpu_times_percent(interval=0))
check(psutil.net_io_counters())
if LINUX and not os.path.exists('/proc/diskstats'):
pass
else:
if not APPVEYOR:
check(psutil.disk_io_counters())
check(psutil.disk_partitions())
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())
示例3: test_oneshot_twice
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_oneshot_twice(self):
# Test the case where the ctx manager is __enter__ed twice.
# The second __enter__ is supposed to resut in a NOOP.
with mock.patch("psutil._psplatform.Process.cpu_times") as m1:
with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2:
p = psutil.Process()
with p.oneshot():
p.cpu_times()
p.cpu_times()
with p.oneshot():
p.cpu_times()
p.cpu_times()
self.assertEqual(m1.call_count, 1)
self.assertEqual(m2.call_count, 1)
with mock.patch("psutil._psplatform.Process.cpu_times") as m:
p.cpu_times()
p.cpu_times()
self.assertEqual(m.call_count, 2)
示例4: test_prcess_iter_w_params
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_prcess_iter_w_params(self):
for p in psutil.process_iter(attrs=['pid']):
self.assertEqual(list(p.info.keys()), ['pid'])
with self.assertRaises(ValueError):
list(psutil.process_iter(attrs=['foo']))
with mock.patch("psutil._psplatform.Process.cpu_times",
side_effect=psutil.AccessDenied(0, "")) as m:
for p in psutil.process_iter(attrs=["pid", "cpu_times"]):
self.assertIsNone(p.info['cpu_times'])
self.assertGreaterEqual(p.info['pid'], 0)
assert m.called
with mock.patch("psutil._psplatform.Process.cpu_times",
side_effect=psutil.AccessDenied(0, "")) as m:
flag = object()
for p in psutil.process_iter(
attrs=["pid", "cpu_times"], ad_value=flag):
self.assertIs(p.info['cpu_times'], flag)
self.assertGreaterEqual(p.info['pid'], 0)
assert m.called
示例5: test_cpu_count
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_cpu_count(self):
logical = psutil.cpu_count()
self.assertEqual(logical, len(psutil.cpu_times(percpu=True)))
self.assertGreaterEqual(logical, 1)
#
if os.path.exists("/proc/cpuinfo"):
with open("/proc/cpuinfo") as fd:
cpuinfo_data = fd.read()
if "physical id" not in cpuinfo_data:
raise unittest.SkipTest("cpuinfo doesn't include physical id")
physical = psutil.cpu_count(logical=False)
if WINDOWS and sys.getwindowsversion()[:2] <= (6, 1): # <= Vista
self.assertIsNone(physical)
else:
self.assertGreaterEqual(physical, 1)
self.assertGreaterEqual(logical, physical)
示例6: test_cmdline
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_cmdline(self):
sys_value = re.sub(' +', ' ', win32api.GetCommandLine()).strip()
psutil_value = ' '.join(psutil.Process().cmdline())
self.assertEqual(sys_value, psutil_value)
# XXX - occasional failures
# def test_cpu_times(self):
# handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION,
# win32con.FALSE, os.getpid())
# self.addCleanup(win32api.CloseHandle, handle)
# sys_value = win32process.GetProcessTimes(handle)
# psutil_value = psutil.Process().cpu_times()
# self.assertAlmostEqual(
# psutil_value.user, sys_value['UserTime'] / 10000000.0,
# delta=0.2)
# self.assertAlmostEqual(
# psutil_value.user, sys_value['KernelTime'] / 10000000.0,
# delta=0.2)
示例7: test_cpu_times
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_cpu_times(self):
fields = psutil.cpu_times()._fields
kernel_ver = re.findall(r'\d+\.\d+\.\d+', os.uname()[2])[0]
kernel_ver_info = tuple(map(int, kernel_ver.split('.')))
if kernel_ver_info >= (2, 6, 11):
self.assertIn('steal', fields)
else:
self.assertNotIn('steal', fields)
if kernel_ver_info >= (2, 6, 24):
self.assertIn('guest', fields)
else:
self.assertNotIn('guest', fields)
if kernel_ver_info >= (3, 2, 0):
self.assertIn('guest_nice', fields)
else:
self.assertNotIn('guest_nice', fields)
示例8: test_procfs_path
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_procfs_path(self):
tdir = tempfile.mkdtemp()
try:
psutil.PROCFS_PATH = tdir
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
self.assertRaises(IOError, psutil.boot_time)
# self.assertRaises(IOError, psutil.pids)
self.assertRaises(IOError, psutil.net_connections)
self.assertRaises(IOError, psutil.net_io_counters)
self.assertRaises(IOError, psutil.net_if_stats)
self.assertRaises(IOError, psutil.disk_io_counters)
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
psutil.PROCFS_PATH = "/proc"
os.rmdir(tdir)
示例9: test_oneshot_twice
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_oneshot_twice(self):
# Test the case where the ctx manager is __enter__ed twice.
# The second __enter__ is supposed to resut in a NOOP.
p = psutil.Process()
with mock.patch("psutil._psplatform.Process.cpu_times") as m1:
with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2:
with p.oneshot():
p.cpu_times()
p.cpu_times()
with p.oneshot():
p.cpu_times()
p.cpu_times()
self.assertEqual(m1.call_count, 1)
self.assertEqual(m2.call_count, 1)
with mock.patch("psutil._psplatform.Process.cpu_times") as m:
p.cpu_times()
p.cpu_times()
self.assertEqual(m.call_count, 2)
示例10: test_misc
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_misc(self):
# XXX this test causes a ResourceWarning on Python 3 because
# psutil.__subproc instance doesn't get propertly freed.
# Not sure what to do though.
cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
with psutil.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as proc:
proc.name()
proc.cpu_times()
proc.stdin
self.assertTrue(dir(proc))
self.assertRaises(AttributeError, getattr, proc, 'foo')
proc.terminate()
if POSIX:
self.assertEqual(proc.wait(5), -signal.SIGTERM)
else:
self.assertEqual(proc.wait(5), signal.SIGTERM)
示例11: test_prcess_iter_w_attrs
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_prcess_iter_w_attrs(self):
for p in psutil.process_iter(attrs=['pid']):
self.assertEqual(list(p.info.keys()), ['pid'])
with self.assertRaises(ValueError):
list(psutil.process_iter(attrs=['foo']))
with mock.patch("psutil._psplatform.Process.cpu_times",
side_effect=psutil.AccessDenied(0, "")) as m:
for p in psutil.process_iter(attrs=["pid", "cpu_times"]):
self.assertIsNone(p.info['cpu_times'])
self.assertGreaterEqual(p.info['pid'], 0)
assert m.called
with mock.patch("psutil._psplatform.Process.cpu_times",
side_effect=psutil.AccessDenied(0, "")) as m:
flag = object()
for p in psutil.process_iter(
attrs=["pid", "cpu_times"], ad_value=flag):
self.assertIs(p.info['cpu_times'], flag)
self.assertGreaterEqual(p.info['pid'], 0)
assert m.called
示例12: test_fields
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_fields(self):
fields = psutil.cpu_times()._fields
kernel_ver = re.findall(r'\d+\.\d+\.\d+', os.uname()[2])[0]
kernel_ver_info = tuple(map(int, kernel_ver.split('.')))
if kernel_ver_info >= (2, 6, 11):
self.assertIn('steal', fields)
else:
self.assertNotIn('steal', fields)
if kernel_ver_info >= (2, 6, 24):
self.assertIn('guest', fields)
else:
self.assertNotIn('guest', fields)
if kernel_ver_info >= (3, 2, 0):
self.assertIn('guest_nice', fields)
else:
self.assertNotIn('guest_nice', fields)
示例13: test_procfs_path
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def test_procfs_path(self):
tdir = self.get_testfn()
os.mkdir(tdir)
try:
psutil.PROCFS_PATH = tdir
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
self.assertRaises(IOError, psutil.boot_time)
# self.assertRaises(IOError, psutil.pids)
self.assertRaises(IOError, psutil.net_connections)
self.assertRaises(IOError, psutil.net_io_counters)
self.assertRaises(IOError, psutil.net_if_stats)
# self.assertRaises(IOError, psutil.disk_io_counters)
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
psutil.PROCFS_PATH = "/proc"
示例14: _sample_cpu_and_memory
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def _sample_cpu_and_memory():
# times = np.asarray(psutil.cpu_times(percpu=True))
# mem = psutil.virtual_memory()
return dict(
time_utc=Time.now().utc.isot,
# memory=dict(total=mem.total,
# inactive=mem.inactive,
# available=mem.available,
# free=mem.free,
# wired=mem.wired),
# cpu=dict(ncpu=psutil.cpu_count(),
# user=list(times[:, 0]),
# nice=list(times[:, 1]),
# system=list(times[:, 2]),
# idle=list(times[:, 3])),
)
示例15: __init__
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import cpu_times [as 别名]
def __init__(self):
self.items = {
"cpu count": psutil.cpu_count() if psutil is not None else -1,
"cpu use": 0.0,
"memory used": 0,
"memory percent": 0.0,
"start time": time.time(),
}
if psutil is not None:
times = psutil.cpu_times()
self.previous_cpu = SystemMonitor.CPUStats(sum(times), times.idle,)
else:
self.previous_cpu = SystemMonitor.CPUStats(0, 0)
self.task = task.LoopingCall(self.update)
self.task.start(1.0)