CPU Usage Per Process in Python

Is it possible for me to see the amount of processor usage (% of maximum) that the current, python, app is using?

Scenario: My host will allow me to run my app as long as it does not consume more then X% of the CPU power, so I would like it to 'keep an eye on itself' and slowdown. So how can I know how much CPU the app is using?

Target platform is *nix, however I would like to do it on a Win host also.

Asked by: Samantha621 | Posted: 05-10-2021

Answer 1

>>> import os
>>> os.times()
(1.296875, 0.765625, 0.0, 0.0, 0.0)
>>> print os.times.__doc__
times() -> (utime, stime, cutime, cstime, elapsed_time)

Return a tuple of floating point numbers indicating process times.

From the (2.5) manual:

times( )

Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children's user time, children's system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. Availability: Macintosh, Unix, Windows.

Answered by: Edward304 | Posted: 06-11-2021

Answer 2

By using psutil:

>>> import psutil
>>> p = psutil.Process()
>>> p.cpu_times()
cputimes(user=0.06, system=0.03)
>>> p.cpu_percent(interval=1)

Answered by: Emily583 | Posted: 06-11-2021

Answer 3

The resource module provides getrusage which can give you the information you need, at least for Unix-like platforms.

Note that CPU usage as a percentage is always measured over a time interval. Essentially, it is the amount of time taken by your program doing something divided by the interval time.

For example, if your application takes 2 seconds of CPU time over a 5 second period, then it can be said to be using 40% of the CPU.

Note that this calculation, as simple as it seems, can get tricky when using a multiprocessor system. If your application uses 7 seconds of CPU time in 5 seconds of wall clock time on a two-processor system, do you say it is uses 140% or 70% CPU?

Update: As gimel mentions, the os.times function also provides this information in a platform-independent way. The above calculation notes still apply, of course.

Answered by: Edgar308 | Posted: 06-11-2021

Answer 4

Use time.clock() to get the CPU time. To get the percentage of CPU usage do CPU time elapsed/time elapsed

For example, if CPU time elapsed is 0.2 and time elapsed is 1 then the cpu usage is 20%.

Note:You have to divide by by number of processers you have. If you have 2 i.e. a dual core:

import decimal,timeit
def torture():
import time
while 1:

Answered by: Elise863 | Posted: 06-11-2021

Similar questions

linux - Simple remote process monitoring with Python

I'd like to write a python script to perform some very simple "agentless" monitoring of remote processes running on linux servers. It would perform the following tasks, in psuedocode: for each remoteIPAddress in listOfIPAddresses: log into server@remoteIPAddress via ssh execute the equivalent of a 'ps -ef' command grep the result to make sure a particular process (by name) is still runni...

winapi - Monitoring a displays state in python?

How can I tell when Windows is changing a monitors power state?

django - Does anyone know of a python based web ui for snmp monitoring?

Closed. This question does not meet Stack Overflow guid...

Should I use Perl or Python for network monitoring?

python - Monitoring user idle time

Developing a mac app, how can I tell whether the user is currently at their computer or not? Or how long ago they last pressed a key or moved the mouse?

python - Monitoring internet activity

I'm looking into writing a small app (in Python) that monitors internet activity. The same idea as NetMeter except with a little more customisation (I need to be able to set off-peak time ranges). Anyway, I've been having a little trouble researching these questions: Does Python have an API to monitor this?...

python - Monitoring a tcp port

For fun, I've been toying around with writing a load balancer in python and have been trying to figure the best (correct?) way to test if a port is available and the remote host is still there. I'm finding that, once connected, it becomes difficult to tell when the remote host goes down. I've turned keep alive on, but can't get it to recognize a downed connection sooner than a minute (I realize polling more often ...

monitoring - How do I watch a folder for changes and when changes are done using Python?

i need to watch a folder for incoming files. i did that with the following help: How do I watch a file for changes? the problem is that the files that are being moved are pretty big (10gb) and i want to be notified when all files are done moving. i tried comparing the size of the folder every 20 seconds but the f...

python - Monitoring files with twisted

Is it possible to write a Twisted application to monitor addtion of new files/folders to a specific folder? If yes, could someone point me to some code/link trying to do it, or give an idea how to do that?

python - is there anyway of monitoring the transfer rate of a urllib2 transfer?

This question already has answers here:

python - Monitoring Celery, what should I use?

Closed. This question does not meet Stack Overflow guid...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python