You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7KB

  1. import platform
  2. import subprocess
  3. import fileinput
  4. def get_mac_cpu_speed():
  5. commond = 'system_profiler SPHardwareDataType | grep "Processor Speed" | cut -d ":" -f2'
  6. proc = subprocess.Popen([commond], shell=True, stdout=subprocess.PIPE)
  7. output = proc.communicate()[0]
  8. output = output.decode() # bytes 转str
  9. speed = output.lstrip().rstrip('\n')
  10. return speed
  11. def get_linux_cpu_speed():
  12. for line in fileinput.input('/proc/cpuinfo'):
  13. if 'MHz' in line:
  14. value = line.split(':')[1].strip()
  15. value = float(value)
  16. speed = round(value / 1024, 1)
  17. return "{speed} GHz".format(speed=speed)
  18. def get_windows_cpu_speed():
  19. import winreg
  20. key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")
  21. speed, type = winreg.QueryValueEx(key, "~MHz")
  22. speed = round(float(speed)/1024, 1)
  23. return "{speed} GHz".format(speed=speed)
  24. def get_cpu_speed():
  25. osname = platform.system() # 获取操作系统的名称
  26. speed = ''
  27. if osname == "Darwin":
  28. speed = get_mac_cpu_speed()
  29. if osname == "Linux":
  30. speed = get_linux_cpu_speed()
  31. if osname in ["Windows", "Win32"]:
  32. speed = get_windows_cpu_speed()
  33. return speed
  34. # print(get_cpu_speed())
  35. import psutil
  36. # CPU逻辑数量
  37. # print(psutil.cpu_count())
  38. # CPU物理核心
  39. # print(psutil.cpu_count(logical=False))
  40. # print(psutil.cpu_percent(interval=1, percpu=True))
  41. #
  42. # print(psutil.virtual_memory())
  43. # print(psutil.virtual_memory().percent)#获取内存使用率
  44. # print(psutil.swap_memory())
  45. # print(psutil.disk_partitions()) # 磁盘分区信息
  46. # print(psutil.disk_usage('/')) # 磁盘使用情况
  47. # print(psutil.disk_io_counters()) # 磁盘IO
  48. print(len(psutil.pids()))