psutil
A cross-platform process and system utilities module for Python
I'm executing an extern program through python, I want to know what is the best choice for calling the outside program, with subprocess.Popen()
or with subprocess.call()
, also I need to measure elapsed time, amount of memory and cpu used by the extern program. I've heard of psutil
, but don't really know which to choose.
Source: (StackOverflow)
For the following code, I get the error, str not callable
. Also I am not able to access any of the functions within the class. When I copy-paste examples straight from the docs, I still get the error.
import psutil
p = psutil.Process(4011)
p.name()
Source: (StackOverflow)
I have an application that works perfectly with my MacBook (Mavericks).
After trying it with a virtual machine (OSX Lion), it crashes with this Traceback:
Mac-OS-X-Lion AppleFileServer[219]: _Assert: /SourceCache/afpserver/afpserver-585.2/afpserver/AFPRequest.cpp, 1502 (-5000)
--- last message repeated 2 times ---
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: Traceback (most recent call last):
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "main.py", line 13, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: import SafeDriveController
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/SafeDriveController.py", line 17, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: from safedrive.sftp import sftp_server
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/safedrive/sftp/sftp_server.py", line 14, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: from safedrive.sftp.gpg_crypter import GpgCrypter
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/safedrive/sftp/gpg_crypter.py", line 3, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: import gnupg
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/gnupg/__init__.py", line 23, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: from . import gnupg
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/gnupg/gnupg.py", line 46, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: from . import _parsers
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/gnupg/_parsers.py", line 34, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: from . import _util
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/gnupg/_util.py", line 32, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: import psutil
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/psutil/__init__.py", line 148, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: import psutil._psosx as _psplatform
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: File "/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/psutil/_psosx.py", line 17, in <module>
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: import _psutil_osx as cext
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: ImportError: dlopen(/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/_psutil_osx.so, 2): Symbol not found: ___strlcat_chk
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: Referenced from: /Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/_psutil_osx.so
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: Expected in: /usr/lib/libSystem.B.dylib
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: in /Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/_psutil_osx.so
Mac-OS-X-Lion SafeDrive[304]: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '/Users/FrancoisB/EFounders/efounders.safedrive/SafeDrive/main.m:79 main() PyRun_SimpleFile failed with file '/Users/user/Public/Drop Box/SafeDrive.app/Contents/Resources/main.py'. See console for errors.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8ea72fc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff91d28d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8ea72dfa +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff8ea72d84 +[NSException raise:format:] + 116
4 SafeDrive 0x000000010000164e main + 1486
5 SafeDrive 0x0000000100001074 start + 52
)
Mac-OS-X-Lion [0x0-0x22022].Affinitic.SafeDrive[304]: terminate called throwing an exception
Mac-OS-X-Lion ReportCrash[306]: Attempting to read data: Called memoryAtAddress: 0x1042b650d, which is in an unmappable portion of [0x0 -> 0xffffffffffffffff] in PID# 304.
Do you have an idea to solve this bug ?
Source: (StackOverflow)
Is there a way to get disk IO and network usage as a percentage by psutil.
I found some useful function. But I don't know, how to get as a percentage using
psutil.disk_io_counters()
psutil.net_io_counters()
Source: (StackOverflow)
The code always returns 0.0 values, regardless of interval values.
import psutil
p = psutil.Process()
print p.cpu_percent(interval=1)
print p.cpu_percent(interval=None)
Source: (StackOverflow)
I am a newbie in python and trying to get an understanding of psutil
module. My question is that if have more than 1 instance of a process (e.g. two instances of VLC media player), does psutil.kill()
kills all the instances of that process or only one of the instances?
Source: (StackOverflow)
Im trying to do some things with python psutil but get a strange error.
procs = psutil.get_process_list()
Gets me the following error:
AttributeError: 'module' object has no attribute 'get_process_list'
The only thing i found about it was this:
https://github.com/giampaolo/psutil/issues/524
But no real solution besides pasting it to another directory (which I tried but is not working for me). Does anyone has a clue why I get this error?
Great thanx in advance!
Source: (StackOverflow)
I have code that looks similar to this:
def memoryIntensiveFunction(x):
largeTempVariable = Intermediate(x)
processFunction(largeTempVariable,x)
The problem is that the variable temp
is something like 500 mb in a test case of mine, but that space is not returned to the OS when memoryIntensiveFunction
is finished. I know this because memory profiling with the guppy
tool says largeTempVariable
is freed (i.e., within Python), but psutil
shows it isn't. I presume I'm seeing the effects described here. The problem is that this process is long running (i.e. hours), memoryIntensiveFunction
is run at the beginning and never again, so it's inconvenient for me to have to carry the 500mb around for hours.
One solution I found here and here suggests using a separate process. Multiprocessing incurs its own costs, but it would be worth it in my case. However, this would require refactoring memoryIntensiveFunction
callers to receive x
as a return value instead of seeing it modified in place. The real killer is that my object x
is not picklable (it makes heavy use of boost python extensions). It would be a lot of work to make x
picklable.
Are there any options I'm not considering?
Source: (StackOverflow)
I have a small python script, that essentially looks like the following:
import os
import psutil
def processtree():
pid = os.getpid()
# have to go two levels up to skip calling shell and
# get to actual parent process
parent = psutil.Process(pid).parent().parent()
print 'Parent %s [PID = %d]' % (parent.name(), parent.pid)
print ' |'
for child in parent.children(recursive=True):
if child.pid != pid:
print ' - Child %s [PID = %d]' % (child.name(), child.pid)
else:
print ' - Child %s [PID = %d] (Self)' % (child.name(), child.pid)
if '__name__' == '__main__':
processtree()
When I run this script in bash
on Windows, with nothing else running, I see the following:
Parent bash.exe [PID = 5984]
|
- Child bash.exe [PID = 5008]
|
- Child python.exe [PID = 3736] (Self)
This information is correct. The parent bash process is PID 5984, and the python process is 3736. Now, I run sleep 10000 &
so that it is running as a child of the PID 5984. I check ps -aef | grep 5984
and it is there;:
$ ps -aef | grep 5984 | grep -v grep | grep -v ps
myuser 5984 1 con May 12 /bin/bash
myuser 5080 5984 con 11:17:12 /bin/sleep
myuser 3948 5984 con 11:36:47 /bin/bash
However, when I run my script again, it still shows:
Parent bash.exe [PID = 5984]
|
- Child bash.exe [PID = 7560]
|
- Child python.exe [PID = 5168] (Self)
It does not show sleep
as a child of the parent bash process, even though ps
is showing it as present.
Note that the PID for the bash.exe child has changed since a new calling shell was created (not sure why this happens, but I don't think it's related). The PID of the python interpreter because I called the script again python processtree.py
.
Not sure what I'm doing wrong, and I've been staring at this for a while. Any help is appreciated...
Source: (StackOverflow)
I use psutil , but seam the F: drive can be seen ...
>>> psutil.disk_partitions()
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpa
drom'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom'), sdiskpa
s='rw,removable'), sdiskpart(device='G:\\', mountpoint='G:\\', fstype='NTFS', opts='r
Source: (StackOverflow)
I am writing a python script to get some basic system stats. I am using psutil for most of it and it is working fine except for one thing that I need.
I'd like to log the average cpu wait time at the moment.
from top output it would be in CPU section under %wa.
I can't seem to find how to get that in psutil, does anyone know how to get it? I am about to go down a road I really don't want to go on....
That entire CPU row is rather nice, since it totals to 100 and it is easy to log and plot.
Thanks in advance.
Source: (StackOverflow)
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.
Source: (StackOverflow)
I'm writing a program that will perform some logging of my company's webserver load. One information I need to log is the server's CPU Load.
I know that CPU Load -- in monit
or top
/htop
-- reflects the total load across cores, normalized against 1 core. So, for an 8 core server, the total load (when it's heavily loaded) will be higher than 100%. And when all cores are running full load, might reach 800%.
However, monit and top/htop also might show oversubscribed CPU Load, in which the CPU Load number is higher than 800%, indicating that jobs are being queued.
I'm interested in such situation.
My questions:
Does the psutil.cpu_times_percent()
function behave the same as monit's/top's CPU Load indicator? That is, will the percentage returned be higher than 100% (cpu_times_percent()
normalizes to 100% instead of to 1 core) if there are jobs being queued?
If the answer to #1 is "no", then how can I detect oversubscription / jobs being queued?
Source: (StackOverflow)
I want to find the path of every running process in windows.
I tried to use the psutil module but it doesn't show me all the paths. It can't find many processes' paths because of the error: "psutil.AccessDenied"
c = wmi.WMI()
for process in c.Win32_Process():
p = psutil.Process(int(process.ProcessId))
try:
path = p.exe()
except:
path = "-"
Is there another way to get a path of a process?
Source: (StackOverflow)
How to get the amount of memory which has been used by a single process in windows platform with psutil library? (I dont want to have the percentage , I want to know the amount in bytes)
We can use:
psutil.virtual_memory().used
To find the memory usage of the whole OS in bytes, but how about each single process?
Thanks,
Source: (StackOverflow)