nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

NoSuchProcess: No process found with pid 6396 #132

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I wrap the get_process_list() in a sleep loop
2. for process in psutil.get_process_list():
          if process.name.find("notepad") >=0:
              print process.name
                print >> resultFile, "win32", process.get_cpu_percent(), process.get_memory_percent()    
3 It throw error at the if process.name.find("notepad") >=0: sentence.

4 The error is 
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 530, in __bootstrap_inner
    self.run()
  File "D:\tudworkspace\OpenTTDTesting\src\performance.py", line 15, in run
    pid = measureCPUandMem(resultFile, pid)
  File "D:\tudworkspace\OpenTTDTesting\src\performance.py", line 24, in measureCPUandMem
    if process.name.find("notepad") >=0:
  File "C:\Python27\lib\site-packages\psutil\__init__.py", line 205, in name
    self.deproxy()
  File "C:\Python27\lib\site-packages\psutil\__init__.py", line 180, in deproxy
    self._procinfo = ProcessInfo(*_platform_impl.get_process_info(self._procinfo.pid))
  File "C:\Python27\lib\site-packages\psutil\_psmswindows.py", line 66, in wrapper
    return callable(*args, **kwargs)
  File "C:\Python27\lib\site-packages\psutil\_psmswindows.py", line 81, in get_process_info
    infoTuple = _psutil_mswindows.get_process_info(pid)
NoSuchProcess: No process found with pid 6396

What is the expected output? What do you see instead?
I expected it doesn't throws any error. 

What version of psutil are you using? What Python version?
Python 2.7. I use the psutil-python2.7 windows installer.

On what operating system? Is it 32bit or 64bit version?
I use 32 bit win 7 profession

Please provide any additional information below.

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()
        pid = 0
        while(self.flag == 1):
            pid = measureCPUandMem(resultFile, pid)
            time.sleep(0.4)

def measureCPUandMem(resultFile, pid):
    print >> resultFile, "-----------" + time.asctime() + "\n" + "%f"%time.time()
    process = psutil.Process(pid) 
    if sys.platform == "win32":
         for process in psutil.get_process_list():
            if process.name.find("notepad") >=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()

    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()
    return pid

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("notepad") >=0:
#                print process.name
#                print "win32", process.get_cpu_percent(), 
process.get_cpu_times(), process.get_memory_percent()
#        time.sleep(0.5)

Original issue reported on code.google.com by siqis...@gmail.com on 8 Nov 2010 at 12:55

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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