get cpu usage python code example

Example 1: python get cpu info

import cpuinfo

print('CPU =', cpuinfo.get_cpu_info()['brand_raw'])

Example 2: How to get current CPU and RAM usage in Python?

#!/usr/bin/env python
import psutil
# gives a single float value
psutil.cpu_percent()
# gives an object with many fields
psutil.virtual_memory()
# you can convert that object to a dictionary 
dict(psutil.virtual_memory()._asdict())

Example 3: python memory usage

import sys
a, b, c,d = "abcde" ,"xy", 2, 15.06
print(sys.getsizeof(a))
print(sys.getsizeof(b))
print(sys.getsizeof(c))
print(sys.getsizeof(d))

#Running the above code gives us the following result
38
35
24
24

Example 4: python cpu usage

# Importing the library
import psutil
  
# Calling psutil.cpu_precent() for 4 seconds
print('The CPU usage is: ', psutil.cpu_percent(4))

Example 5: how to measure how much your of cpu your program is using in python

$ pip install memory_profiler

Example 6: get cpu usage from url in pyrhon

import psutil