Did I find the right examples for you? yes no

# psutil.cpu_percent

All Samples(74)  |  Call(73)  |  Derive(0)  |  Import(1)
```Return a float representing the current system-wide CPU
utilization as a percentage.

When interval is > 0.0 compares system CPU times elapsed before
and after the interval (blocking).

When interval is 0.0 or None compares system CPU times elapsed
since last call or module import, returning immediately.
In this case is recommended for accuracy that this function be
called with at least 0.1 seconds between calls.(more...)
```

```        def cpu_percent(interval=0.1, percpu=False):
"""Return a float representing the current system-wide CPU
utilization as a percentage.

When interval is > 0.0 compares system CPU times elapsed before
and after the interval (blocking).

When interval is 0.0 or None compares system CPU times elapsed
since last call or module import, returning immediately.
In this case is recommended for accuracy that this function be
called with at least 0.1 seconds between calls.

When percpu is True returns a list of floats representing the
utilization as a percentage for each CPU.
First element of the list refers to first CPU, second element
to second CPU and so on.
The order of the list is consistent across calls.

Examples:

>>> # blocking, system-wide
>>> psutil.cpu_percent(interval=1)
2.0
>>>
>>> # blocking, per-cpu
>>> psutil.cpu_percent(interval=1, percpu=True)
[2.0, 1.0]
>>>
>>> # non-blocking (percentage since last call)
>>> psutil.cpu_percent(interval=0)
2.9
>>>
"""
global _last_cpu_times
global _last_per_cpu_times
blocking = interval is not None and interval > 0.0

def calculate(t1, t2):
t1_all = sum(t1)
t1_busy = t1_all - t1.idle

t2_all = sum(t2)
t2_busy = t2_all - t2.idle

# this usually indicates a float precision issue
if t2_busy <= t1_busy:
return 0.0

busy_delta = t2_busy - t1_busy
all_delta = t2_all - t1_all
busy_perc = (busy_delta / all_delta) * 100
return round(busy_perc, 1)

# system-wide usage
if not percpu:
if blocking:
t1 = cpu_times()
time.sleep(interval)
else:
t1 = _last_cpu_times
_last_cpu_times = cpu_times()
return calculate(t1, _last_cpu_times)
# per-cpu usage
else:
ret = []
if blocking:
tot1 = cpu_times(percpu=True)
time.sleep(interval)
else:
tot1 = _last_per_cpu_times
_last_per_cpu_times = cpu_times(percpu=True)
for t1, t2 in zip(tot1, _last_per_cpu_times):
ret.append(calculate(t1, t2))
return ret
```

```
# cpu usage
percs = psutil.cpu_percent(interval=0, percpu=True)
for cpu_num, perc in enumerate(percs):
dashes, empty_dashes = get_dashes(perc)
```

```
# cpu usage
for cpu_num, perc in enumerate(psutil.cpu_percent(interval=0, percpu=True)):
dashes, empty_dashes = get_dashes(perc)
print_line(" CPU%-2s [%s%s] %5s%%" % (cpu_num, dashes, empty_dashes,
```

```    def _get_cpu_utilization(self):
'''This will return per-CPU utilization'''
try:
cpu_util = psutil.cpu_percent(interval=0, percpu=True)
cpu_util_agg = psutil.cpu_percent(interval=0)
```

```        if per_cpu_test in affirm:
self.PER_CPU = True
self.cpu_percent = psutil.cpu_percent(interval=1,percpu=True)
else:
self.PER_CPU = False
self.cpu_percent = psutil.cpu_percent(interval=1)
```
```    def collect_cpu_percent(self):
per_check = bool(self.PER_CPU or self.CPUS)
self.cpu_percent = psutil.cpu_percent(interval=0,percpu=per_check)

def aggregate_data(self):
```

```    def run(self):
while not self.terminated:
val = psutil.cpu_percent(interval=self.interval, percpu=False)
cpu_times = psutil.cpu_times(percpu=True)
try:
```
```    def run(self):
while not self.terminated:
result = []
per_val = psutil.cpu_percent(interval=self.interval, percpu=True)
per_cpu_times = psutil.cpu_times(percpu=True)
```

```from hd44780 import HD44780
from socket import gethostname, gethostbyname_ex
from psutil import cpu_percent, phymem_usage
from time import sleep

lcd = HD44780()

while 1:
sleep(2)
cpu = round(cpu_percent(),0)
```

```    dt = datetime.datetime.fromtimestamp
host_props["boottime"] = dt(psutil.get_boot_time())
host_props["cpu_used"] = psutil.cpu_percent(interval=1, percpu=False)
host_props["hardwareisa"] = platform.processor()
host_props["hardwaremodel"] = platform.processor()
```

```    global _cpu_percent_called
if not _cpu_percent_called:
psutil.cpu_percent(interval=0, percpu=True)
_cpu_percent_called = True
time.sleep(0.1)

ret = 0
for core_usage in psutil.cpu_percent(interval=0, percpu=True):
```

```
# Get the CPU usage.
cpu = psutil.cpu_percent()

# Run the input through the model and shift the resulting prediction.
```

```    def measure(self, variant=None):
return [x / 100 for x in psutil.cpu_percent(interval=0, percpu=True)]

@plugin
```

1 | 2 | 3 | 4 | 5 | 6  Next