psutil

Python3 获取系统资源

徘徊边缘 提交于 2019-12-04 20:51:54
cpu disk mem import osimport psutilos.chdir(os.getcwd()) #cpu def get_cpu_info(): cpu_percent=psutil.cpu_percent(interval=1) cpu_info="used cpu perence is : %0.2f%%" % cpu_percent, #print("used cpu perence is : %0.2f%%" % cpu_percent) return cpu_info #memory def get_memory_info(): virtual_memory = psutil.virtual_memory() used_memory = virtual_memory.used / 1024 / 1024 free_memory = virtual_memory.free / 1024 / 1024 memory_percent = virtual_memory.percent #print("memory perence is:%0.2f%%" % memory_percent,"usedmemory is:%0.2f M" % used_memory) mem_info="memory perence is:%0.2f%%" % memory

python监控cpu 硬盘 内存

隐身守侯 提交于 2019-12-04 06:34:17
import psutil import time import yagmail def sendmail(subject,contents): yag = yagmail.SMTP(user='155xxx8589@163.com',password='邮箱授权码',host='smtp.163.com') yag.send(to='15534828589@163.com',subject=subject,contents=contents) yag.close() def cpu(): cpu = psutil.cpu_percent(1) return {'cpu_percent':cpu} def mem(): mem_total = psutil.virtual_memory()[0] mem_percent = psutil.virtual_memory()[2] return {'mem_total':int(mem_total/1024/1024),'mem_percent':mem_percent} def disk(): disk_total = psutil.disk_usage('c:')[0] disk_percent = psutil.disk_usage('c:')[3] return {'disk_total':int(disk_total/1024

How to get the percentage of memory usage of a process?

时间秒杀一切 提交于 2019-12-04 04:06:50
Using the following code, I can get the memory consumption of a give process in MiB: def memory_usage_psutil(): # return the memory usage in MB import psutil process = psutil.Process(os.getpid()) mem = process.get_memory_info()[0] / float(2 ** 20) return mem How can I change this to return the percentage of memory consumption? Update : I need to get the current value of %MEM column when executing the top command in terminal for a specific process. Example : I need this function to return 14.2 for the process id of VirtualBox process. use process.memory_percent() This agrees with top. In the

Error in cx_freeze Build

匿名 (未验证) 提交于 2019-12-03 09:06:55
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using cx_freeze to bundle my app. I have 2 questions. 1) I am using OSX Mountain Lion - the /build/exe.macosx-10.8-x86_64-2.7 that I have - will it contain executables for Windows, Linux as well - and if so, where in the list can I find it? My original script is cpu.py. The dir contains a lot of ".so" files and 1 "cpu" file. 2) When I try "cpu" from the list above, I get this output. 'psutil' is a Python module required for the script - cpu.py. Desktop/build/exe.macosx-10.8-x86_64-2.7/cpu ; exit; Traceback (most recent call last): File

Python运维之获取操作系统的内存信息

吃可爱长大的小学妹 提交于 2019-12-03 04:40:54
要使用Python获取计算机的内存信息需要用到psutil模块,能够获得的内存信息有内存总量、可用内存总量、已用内存量、内存使用百分比等信息。 写一个简单的小脚本,代码如下: #!/usr/bin/python import psutil pc_mem =psutil.virtual_memory() div_gb_factor =(1024.0 ** 3) print("totalmemor: %fGB" % float(pc_mem.total/div_gb_factor)) print("availablememory: %fGB" % float(pc_mem.available/div_gb_factor)) print("usedmemory: %GB" % float(pc_mem.used/div_gb_factor)) print("percentof used memory: %f" % float(pc_mem.percent)) print("freememory:%fGB" % float(pc_mem.free/div_gb_factor)) 程序运行结果如下; In [1]: %runos_mem.py total memor:15.868237GB available memory:13.069672GB used memory:2.79856B

How to get the max memory usage of a program using psutil in Python

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using the following code to get the max memory usage of the program. import os, subprocess , psutil def mem(cmd): try: with open('in.txt','r') as infile, open('out.txt', 'w') as outfile: p=psutil.Popen("./"+cmd,shell=False,stdin=infile,stdout = outfile) print p.memory_info() except Exception: print "Error" cmd=raw_input() mem(cmd) The problem is sometimes for initial runs of the program the memory usage output is (0,0) but subsequently it displays the correct output. I dont why this happening. For some programs such as the hello world

How to terminate process from Python using pid?

匿名 (未验证) 提交于 2019-12-03 02:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to write some short script in python which would start another python code in subprocess if is not already started else terminate terminal & app (Linux). So it looks like: #!/usr/bin/python from subprocess import Popen text_file = open(".proc", "rb") dat = text_file.read() text_file.close() def do(dat): text_file = open(".proc", "w") p = None if dat == "x" : p = Popen('python StripCore.py', shell=True) text_file.write( str( p.pid ) ) else : text_file.write( "x" ) p = # Assign process by pid / pid from int( dat ) p.terminate() text

Zombie process running xlwings

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I get a python error when attempting to use xlwings. Just importing xlwings seems to be sufficient to trigger the problem. Lewis-MacBook-Air:~ lewis$ python Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Oct 19 2015, 18:31:17) [GCC 4.2.1 (Apple Inc. build 5577)] on darwin Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org >>> import xlwings >>> xlwings.__version__ '0.6.0' >>> exit() Error in atexit.

psutil 模块的功能

匿名 (未验证) 提交于 2019-12-03 00:37:01
一、psutil模块 1. psutil是一个跨平台库( http://code.google.com/p/psutil/ ),能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要应用于系统监控,分析和限制系统资源及进程的管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统, 2.安装 wget https: / /pypi.python.org/packages / source /p /psutil/psutil - 2.0 . 0 .tar.gz tar -xzvf psutil- 2.0 . 0 .tar.gz cd psutil- 2.0 . 0 python setup.py install 3.使用 获取系统性能信息(CPU,内存,磁盘,网络) 3.1CPU相关 查看cpu信息 import Psutil 查看cpu所有信息 >>> psutil.cpu_times() scputimes(user= 11677.09 , nice=

还是环境问题,关于安装psutil

匿名 (未验证) 提交于 2019-12-03 00:03:02
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_VERSION=563 -DPSUTIL_LINUX=1 -I/home/lxg/02dev/py3.6env/include -I/usr/include/python3.6m -c psutil/_psutil_common.c -o build/temp.linux-x86_64-3.6/psutil/_psutil_common.o psutil/_psutil_common.c:9:20: fatal error: Python.h: 没有那个文件或目录 compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 但试过后,解决不料问题 最后解决方法: pip install psuitl 原因: 我目前的env环境是3.6的. 参考: https://github.com/giampaolo