Closed GoogleCodeExporter closed 8 years ago
when I uncomment the code in the main function, and comment the first two lines.
Everything is correct, why?
Original comment by siqis...@gmail.com
on 8 Nov 2010 at 12:58
This should be due to the fact that a certain process exists at the time you
call get_process_list() but then disappears when you call process.name.
Instead of get_process_list() you should iterate over processes by using
process_iter(), which is designed to alleviate exactly this problem:
for proc in psutil.process_iter():
proc.name
Note that you might still incur in the same race condition though, as a process
can disappear at any time, hence what you actually might want to do is this:
for proc in psutil.process_iter():
try:
proc.name
except psutil.NoSuchProcess:
pass
Original comment by g.rodola
on 8 Nov 2010 at 1:14
Thank you Giampaolo. It doesn't prompt the problem again. But Also there is
another problem now. It show that the cpu usage of one of my process is 0%,
but I see from the taskmanager the cpu usage is 25%. How could I handle this
problem
The key lines are
------------------------------
if process.name.find("openttd") >=0:
print process.name
print >> resultFile, "win32", process.get_cpu_percent(), process.get_memory_percent()
print "win32", process.get_cpu_percent(), process.get_cpu_times(), process.get_memory_percent()
except psutil.NoSuchProcess:
pass
-----------------------------------------
Below is my code
import os,sys,time,threading,psutil
from threading import Timer
class Performance(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.flag = 1
def run(self):
filename = os.path.join(".." ,"test_result", "cpumem")
resultFile = open(filename,"a")
measureNetwork()
while(self.flag == 1):
measureCPUandMem(resultFile)
time.sleep(0.4)
def measureCPUandMem(resultFile):
print >> resultFile, "-----------" + time.asctime() + "\n" + "%f"%time.time()
if sys.platform == "win32":
for process in psutil.process_iter():
try:
if process.name.find("openttd") >=0:
print process.name
print >> resultFile, "win32", process.get_cpu_percent(), process.get_memory_percent()
print "win32", process.get_cpu_percent(), process.get_cpu_times(), process.get_memory_percent()
except psutil.NoSuchProcess:
pass
elif sys.platform.find("linux") >=0:
file = os.popen("ps aux|grep openttd")
lines = file.readlines()
for line in lines:
print >> resultFile, line
print >> resultFile, "-----------\n"
resultFile.flush()
def networkWork():
if sys.platform == "win32":
file = os.popen("ps aux -W")
lines = file.readlines()
for line in lines:
if line.find("C:\\\"Program Files\"\\Wireshark\\tshark") >=0:
return
os.system("C:\\\"Program Files\"\\Wireshark\\tshark -b filesize:1000000 -f ip -q -w ..\\test_result\\packages")
else:
file = os.popen("ps aux|grep tshak")
lines = file.readlines()
if(len(lines) >=2):
return
os.system("tshark -b filesize:1000000 -f ip -q -w ../test_result/packages &")
def measureNetwork():
t = threading.Thread(None, networkWork)
t.start()
if __name__ == "__main__":
abc = Performance()
abc.start()
# while True:
# for process in psutil.get_process_list():
# if process.name.find("openttd") >=0:
# print process.name
# print "win32", process.get_cpu_percent(),
process.get_cpu_times(), process.get_memory_percent()
# time.sleep(0.5)
Original comment by siqis...@gmail.com
on 8 Nov 2010 at 2:38
From get_cpu_percent documentation:
get_cpu_percent()
Compare process times to system time elapsed since last call and calculate CPU
utilization as a percentage. It is recommended for accuracy that this function
be called with at least 1 second between calls. The initial delta is calculated
from the instantiation of the Process object.
...hence, you can try to:
- call get_cpu_percent()
- time.sleep(0.5)
- call get_cpu_percent() again
Second get_cpu_percent() call will return a meaningful result.
Original comment by g.rodola
on 8 Nov 2010 at 3:16
Thank you, I am wonder why use a proc like file to record all the cputime and
elaspedtime of process every tick, and We can get the correct get_cpu_percent()
now.
I have used PsList to get the percent usage by parse the output constantly, but
I also want to use your library. It is a more elegant way to get the
performance.
Original comment by siqis...@gmail.com
on 8 Nov 2010 at 3:37
Original issue reported on code.google.com by
siqis...@gmail.com
on 8 Nov 2010 at 12:55