You received this message because you are subscribed to a topic in the Google Groups "psutil" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psutil/bEIq0c2ZN5U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psutil+un...@googlegroups.com.
import psutilimport osimport timep = psutil.Process(4048)for i in range(60):time.sleep(1)print p.get_cpu_percent(interval=0.1)
--
I have the same result using only "interval", "interval=0" and sleep(1) and different numbers inbetween. The reason I put time.sleep there was because I thought maybe the interval parameter had some caching that messed up the values.Same (equivalent) result with the script you posted.

--
--
Yes, but that is with very small numbers. Just ran the script on a process with about 4% in Task Manager and it reports as 25% from psutil. The difference is relative to the actual values.I didn't take 60 screenshots but I looked at the Task Manager the entire time. I know it is not very scientific but my observation was pretty easy to read anyway.My use-case for this is the following:We host several different types of game servers. Each game behaves differently. On top of that each modification of the game and even different maps being run require different amount of resources from the computer. Instead of reading these numbers by hand in Task Manager (requires the servers to be full, running that specific game, map etc. Virtually impossible) I want to rely on data.If I can read CPU (it doesn't have to be percent, but it has to be a number I can compare to a maximum) I can determine the usage requirements of a game and provision game server instances over my available hardware using logic, rather than me guessing which server should be able to run the game or not.So, even if an alarm on every server reporting when the CPU (overall) is too high what I really want is the CPU pattern for that specific game given certain amount of players (the two biggest factors usually).As it stands now, the process that gave me 4% in Task Manager was a server with 34 players on it. According to psutil it was at 25% which would mean I could only run a total of 136 slots. We have been running well over 256 on the same hardware without even being close to 100% (Task Manager). Actually we stop loading servers when we reach 70% in order to have some head-room and based on spikes in user behavior.Given this, what would you suggest I do? Use processor time? Maybe use wmic, wmi or some other native tool for Windows? When you say I don't understand the numbers, please give me an idea of what number I actually should look at. I don't know much details about CPU technology but have a fair background and experience in programming.Kind regards,Martin
#!/usr/bin/python"""Example of using process and CPU times from psutil to calculate process CPU %utilizaton. The example here calculates the utilization over a user definedtime interval."""#psutil syntax changes between versions 1 and 2 can create deprecation warnings#the following two lines would enable ignoring the warnings...#import warnings#warnings.filterwarnings('ignore',category=DeprecationWarning)
import psutilimport timeimport re
#instead of ignorng warnings, check which psutil version and call accordinglyver=psutil.__version__verChk1=re.match('1.[0-9].[0-9]',ver) #check if version 1 or notif verChk1 != None: psutilVer=1else: psutilVer=2
#define calculate CPU percentage functiondef get_cpu_pct(pid,pr_procTimes,pr_cpuTimes): #find the given process p=psutil.Process(pid) #compute the change in process utilization time since previous call procTimes=p.get_cpu_times() if psutilVer==1 else p.cpu_times() procTimesTotal=procTimes.user+procTimes.system pr_procTimesTotal=pr_procTimes.user+pr_procTimes.system deltaProcTimes=procTimesTotal-pr_procTimesTotal #compute the change in CPU time since previous call cpuTimes=psutil.cpu_times() cpuTimesTotal=cpuTimes.user+cpuTimes.system+cpuTimes.idle #need to consider idle time as well when computing total CPU time pr_cpuTimesTotal=pr_cpuTimes.user+pr_cpuTimes.system+pr_cpuTimes.idle deltaCPUTimes=cpuTimesTotal-pr_cpuTimesTotal #avoid divison by zero if deltaCPUTimes > 0: #compute the CPU % utilization cpupct=float(deltaProcTimes)/float(deltaCPUTimes)*100 else: #set cpupct to zero if denominator is zero for some reason cpupct=0
#return process and cpu times for next iteration along with CPU % utilized return procTimes,cpuTimes,cpupct #Begin main programpid=int(raw_input('Enter PID: ')) #ask user for a process ID numberproc=psutil.Process(pid) #get the process info for this PID
#initialize historypr_procTimes=proc.get_cpu_times() if psutilVer==1 else proc.cpu_times()pr_cpuTimes=psutil.cpu_times()
#Loop to show progressNloops=10 #number of times to display resultsinterval=1 #number of seconds in the time interval between CPU% calls
#loop to observe process CPU % utilization over timefor i in range(Nloops): strt_tm=time.time() #start elapsed time counter time.sleep(interval) pr_procTimes, pr_cpuTimes, cpupct=get_cpu_pct(pid,pr_procTimes,pr_cpuTimes) print "CPU utilization for process: %s is %.2f" % (pid,cpupct),"%"," Elapsed time: %.3f" % (time.time()-strt_tm)," seconds"