python控制cpu使用率
以下亲测可行。
使用方法:命令行模式
runing.py -c 2 -t 0.01
-c 指定cpu核数:不指定-c参数默认为所有核数。
-t 数值越大,cpu使用率越低。
runing.py
"""
runing 100
"""
import time
from time import clock
import argparse
from multiprocessing import Process
from multiprocessing import cpu_count
import math def exec_func(bt): while True:
for i in range(0, 9600000):
pass
time.sleep(bt) if __name__ == "__main__": parse = argparse.ArgumentParser(description='runing') parse.add_argument(
"-c",
"--count",
default= cpu_count(),
help='cpu count'
) parse.add_argument(
"-t",
"--time",
default= 0.01,
help='cpu time'
) args = parse.parse_args() cpu_logical_count = int(args.count) cpu_sleep_time = args.time try:
cpu_sleep_time = int(args.time)
except ValueError:
try:
cpu_sleep_time = float(args.time)
except ValueError as ex:
raise ValueError(ex) print('\n====================占用CPU核数{}.===================='.format(cpu_logical_count))
print('\n资源浪费starting......')
print(cpu_sleep_time) try: p = Process(target=exec_func, args=("bt",)) ps_list = [] for i in range(0, cpu_logical_count):
ps_list.append(Process(target=exec_func, args=(cpu_sleep_time,))) for p in ps_list:
p.start() for p in ps_list:
p.join()
except KeyboardInterrupt:
print("手工结束!")
python控制cpu使用率的更多相关文章
- 生成CPU使用率 sin 曲线 控制cpu使用率 编程之美
入职Oracle 以后想着把之前写过的<编程之美>中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间. 程序早就写好了. 最终有机会贴出来了.o(∩∩) ...
- python指定cpu使用率,与内存占用率
python指定cpu使用率,与内存占用率 """ runing.py -c 2 -t 0.01 -m 1000 -c cpu核数,不加-c参数为最大核数 -t cpu运 ...
- 开发的时候,一定要及时控制CPU使用率以及使用内存大小等三个问题(一个星期检查一次)
一直专注于功能的开发,没注意CPU和内存.昨天无意中发现两个问题: 1. 程序启动后,什么都没干,CPU就50%了(单核).现在想找原因降低使用率,感觉无从下手,要是平时就注意这个问题就好了. 2. ...
- Linux 控制CPU使用率
曾经看过<编程之美>上提到说使 CPU的使用率固定在百分之多少.然后这次刚好要用到这个东西,下面是一个简单的实现.基于多线程: Linux 版本: #include <iostrea ...
- python获取每颗cpu使用率
以下是关于python来获取服务器每颗CPU使用率的使用脚本. #!/usr/bin/env python # -*- coding: utf-8 -*- import re,time def _re ...
- [Python Study Notes]计算cpu使用率v0.1
V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 '''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]计算cpu使用率
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- Python获取CPU、内存使用率以及网络使用状态代码
Python获取CPU.内存使用率以及网络使用状态代码_python_脚本之家 http://www.jb51.net/article/134714.htm
- 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等
方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...
随机推荐
- man mkfs
---恢复内容开始--- MKFS(8) MKFS(8) NAME/名称 ...
- windows openssh安装
下载地址:https://github.com/PowerShell/Win32-OpenSSH/releases 解压好后打开目录,执行以下命令: powershell.exe -Execution ...
- [BZOJ] 书堆
问题描述 蚂蚁是勤劳的动物,他们喜欢挑战极限?现在他们迎来了一个难题!蚂蚁居住在图书馆里,图书馆里有大量的书籍.书是形状大小质量都一样的矩形.蚂蚁要把这些书摆在水平桌子的边緣.蚂蚁喜欢整洁的布置,所以 ...
- socket函数库简单封装
#pragma once #ifndef WINSOCK_H #include<WinSock2.h> #pragma comment(lib,"ws2_32.lib" ...
- WPF 设置窗体大小为显示器工作区域大小
最近做的项目遇到一个问题,窗体在1680*1050分辨率下显示,系统字体设置为小字体时,显示没问题,但是调到中等字体时,窗体显示位置出了问题.主窗体为无边框窗体,拖动及放大缩小事件都是自己写的. ...
- next_permutation():按字典序输出下一个排列
#include<iostream> #include<algorithm> using namespace std; int main() { int data[4]={5, ...
- 12 October
次小生成树 http://poj.org/problem?id=1679 不难得出,次小生成树可以由最小生成树更换一条边得到. 首先构造原图的最小生成树,然后枚举每一条不在最小生成树中的边 (u, v ...
- vue中操作Dom节点的方法
1.vue中ref操作dom节点 <template> <div id="app"> <div </div> <button @cl ...
- php面试专题---10、网络协议考点
php面试专题---10.网络协议考点 一.总结 一句话总结: 网络的考点其实就是这些:常见状态码,常见协议,osi七层模型,http和https 1.HTTP/1.1中,状态码200.301.304 ...
- C#如何获取系统downloads和documents路径
https://stackoverflow.com/questions/7672774/how-do-i-determine-the-windows-download-folder-path 如果你通 ...