python中一些常用函数的实现
来源:互联网 发布:加工中心圆怎么编程 编辑:程序博客网 时间:2024/05/24 22:41
python中一些常用函数的实现
1. 实现函数超时设置
# subprocess open wrapper(with timeout)def SystemCallWithTimeout(command, timeout=5): proc = subprocess.Popen(command, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) poll_seconds = 0.5 deadline = time.time() + timeout while time.time() < deadline and proc.poll() == None: time.sleep(poll_seconds) if proc.poll() == None: return '', '' stdout, stderr = proc.communicate() return stdout, stderr
2. 获取进程的pid
#return pid if found, return 0 if notdef GetPid(processName, excludedList = [], machineIP = ''): cmd ='' processName = processName.replace('[', '\[') processName = processName.replace(']', '\]') processName = '\'' + processName + '\'' if (machineIP): cmd = 'ssh ' + machineIP + ' \'ps aux | grep ' + processName + '\'' else: cmd = 'ps aux | grep ' + processName for excludeName in excludedList: cmd += ' | grep -v %s' %excludedName cmd += ' | grep -v grep | awk \'{print $2}\'' result = SystemCall(cmd) if (not result): result = 0 return result3. 获取进程的cpu和memory利用率
#return process cpu and memory percentagedef GetProcessCPUMem(processName, machineIP = ''): cmd ='' if (machineIP): cmd = 'ssh ' + machineIP + '\'ps aux | grep ' + processName + '\'' else: cmd = 'ps aux | grep -w ' + processName cmd += ' | grep -v grep | awk \'{print $3, $6}\''; result = SystemCall(cmd) if (not result): error("No process: %s in machine: %s" %(processName, machineIP), True) return 0,0 fst = result.split(' ') # cpu 100 means 1 cpu core used 100% # mem unit is MB cpu ,mem = float(fst[0]), float(fst[1])/1024.0 return cpu, mem
0 0
- python中一些常用函数的实现
- Python实现的一些常用日期函数
- python中一些常用的函数命令
- python 中常用不常见的一些函数
- python一些常用函数
- Python的一些常用高级函数
- python中常用的函数
- python中一些常用的module小结
- Python中常用的一些模块
- 实现字符串的一些常用字符串函数
- MSQL中常用的一些字符串函数
- 网络编程中常用的一些函数
- sql中一些常用的函数
- c语言中一些常用的函数
- matlab中一些常用的函数
- Unity3D中常用的一些函数
- 线程中一些常用的函数
- JMeter中常用的一些函数
- 使用LinkedList
- stm32定时器的pwm1模式和pwm2模式的区别
- Sublime Text插件之JavaScript Completions
- OpenSessionInViewFilter 的配置及作用
- cocos2d-x环境配置教程
- python中一些常用函数的实现
- Ultra Array
- 免费随书光盘下载
- Array(6) -- Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix I, II
- C语言宏定义,内置宏,__FILE__,__LINE__,## 用法
- 统计学习方法之感知机对偶形式Java实现代码
- 电信光纤——光猫——路由器,如何映射,外网访问
- UVa - 10048 - Audiophobia ( Floyd 变形 )
- 将 FOREIGN KEY 约束 'FK_dbo.Employees_dbo.Admins_AdminID' 引入表 'Employees' 可能会导致循环或多重级联路径。请指定 ON DELETE