# Importing socket library
import socket
import psutil
import time
import sys
# Function to display hostname and
# IP address
def get_cpu():
try:
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
current_cpu = psutil.cpu_percent()
starttime = time.time()
while True:
print("get_cpu:", current_cpu, "host:", host_name, "IP: ", host_ip)
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
except:
print("Unable to get Hostname and IP and Current CPU")
# Driver code
get_cpu() # Function call
# 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_memo
我当前的脚本每60秒打印一次CPU使用情况,
预期输出:获取一个小时的平均CPU使用率,并在该小时内显示最高峰值。
谢谢