企业门户网站功能描述,如何选择扬中网站建设,兰州网络推广关键词,扶贫网站建设python 让cpu满载发布时间#xff1a;2018-07-29 17:25:11编辑#xff1a;admin阅读(5939)搞zabbix监控的时候#xff0c;linux服务器的负载很低#xff0c;如何写一个python脚本#xff0c;让它满载呢#xff1f;网上搜了一堆#xff0c;发现各种不靠谱。后来终于发现了…python 让cpu满载发布时间2018-07-29 17:25:11编辑admin阅读(5939)搞zabbix监控的时候linux服务器的负载很低如何写一个python脚本让它满载呢网上搜了一堆发现各种不靠谱。后来终于发现了更简单的方法就是定义一个死循环函数让函数什么都不做就好了def deadloop():while True:passdeadloop()就是这么简单的几行就能让cpu的一个线程满载执行之后查看top[rootlocalhost ~]# toptop - 17:13:36 up 5:24, 8 users, load average: 0.23, 0.08, 0.06Tasks: 132 total, 2 running, 130 sleeping, 0 stopped, 0 zombie%Cpu0 : 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu3 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 3874004 total, 2856660 free, 410604 used, 606740 buff/cacheKiB Swap: 2097148 total, 2097148 free, 0 used. 3183588 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME COMMAND7519 root 20 0 122852 5496 2428 R 99.3 0.1 0:13.11 python36439 root 20 0 862228 49056 8776 S 1.3 1.3 0:22.24 python37520 root 20 0 157752 2236 1576 R 0.3 0.1 0:00.01 top发现只有一个cpu是满载的使用w查看负载还是很低[rootlocalhost ~]# w17:14:35 up 5:25, 8 users, load average: 0.72, 0.25, 0.12如何让4个cpu都满载呢使用多进程那么开几个进程合适呢根据CPU核心数来获取CPU核心数写一个测试脚本from multiprocessing import cpu_countprint(cpu_count())执行输出4使用多进程并结合CPU核心数from multiprocessing import cpu_countfrom multiprocessing import Processdef func(): # 死循环函数,让cpu满载while True:passif __name__ __main__:p_lst [] # 定义一个列表core_count cpu_count() # CPU核心数for i in range(core_count):p Process(targetfunc) # 子进程调用函数p.start() # 启动子进程p_lst.append(p) # 将所有进程写入列表中for p in p_lst: p.join() # 检测p是否结束,如果没有结束就阻塞直到结束,否则不阻塞print(结束)执行此脚本并查看top状态[rootlocalhost ~]# toptop - 17:18:52 up 5:30, 8 users, load average: 0.11, 0.20, 0.14Tasks: 134 total, 5 running, 129 sleeping, 0 stopped, 0 zombie%Cpu0 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st%Cpu3 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 3874004 total, 2915672 free, 351628 used, 606704 buff/cacheKiB Swap: 2097148 total, 2097148 free, 0 used. 3242624 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME COMMAND7564 root 20 0 132664 6040 1192 R 100.0 0.2 0:08.65 python37566 root 20 0 132664 6000 1152 R 100.0 0.2 0:08.64 python37565 root 20 0 132664 5988 1140 R 99.7 0.2 0:08.63 python37567 root 20 0 132664 6004 1156 R 99.7 0.2 0:08.63 python3查看cpu使用率发现4个python3进程都是100%等待1分钟查看负载发现CPU负载在不断上升[rootlocalhost ~]# w17:20:32 up 5:31, 8 users, load average: 3.29, 1.29, 0.54USER TTY FROM LOGIN IDLE JCPU PCPU WHATroot tty1 16:51 23:28 0.56s 0.56s -bash服务器的风扇在呼呼的叫。先停止吧死机就完蛋了关键字上一篇没有了