[Original] Python get CPU information
# Get CPU running status def cpuInfo(): """ Get CPU running status :return: """ cpuTimes = psutil.cpu_times() # Get memory information in CPU information def memoryInfo(memory): print(memory) """ Get memory information in CPU information :param memory: :return: """ return { 'Total memory(total)': kmgt(memory.total), 'used (used)': kmgt(memory.used), 'free(free)': kmgt(memory.free), 'Usage (percent)': str(memory.percent) + '%', '可用(available)': kmgt(memory.available) if hasattr(memory, 'available') else '', '活跃(active)': kmgt(memory.active) if hasattr(memory, 'active') else '', 'inactive': kmgt(memory.inactive) if hasattr(memory, 'inactive') else '', 'kernel used(wired)': kmgt(memory.wired) if hasattr(memory, 'wired') else '' } return { 'Number of physical CPUs': psutil.cpu_count(logical=False), 'Number of logical CPUs': psutil.cpu_count(), 'CPU usage': psutil.cpu_percent(percpu=True), 'Virtual memory': memoryInfo(psutil.virtual_memory()), 'Swap memory': memoryInfo(psutil.swap_memory()), 'The system starts up to the current moment': { pro: getattr(cpuTimes, pro) for pro in dir(cpuTimes) if pro[0:1] != '_' and pro not in ('index', 'count') }, }
View CPU and memory information in the current operating environment