Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchProcess: No process found with pid 6396 #132

Closed
giampaolo opened this issue May 23, 2014 · 6 comments
Closed

NoSuchProcess: No process found with pid 6396 #132

giampaolo opened this issue May 23, 2014 · 6 comments

Comments

@giampaolo
Copy link
Owner

From siqis...@gmail.com on November 08, 2010 13:55:41

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: http://code.google.com/p/psutil/issues/detail?id=132

@giampaolo
Copy link
Owner Author

From siqis...@gmail.com on November 08, 2010 04:58:33

when I uncomment the code in the main function, and comment the first two lines.
Everything is correct, why?

@giampaolo
Copy link
Owner Author

From g.rodola on November 08, 2010 05:14:02

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

@giampaolo
Copy link
Owner Author

From siqis...@gmail.com on November 08, 2010 06:38:23

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)

@giampaolo
Copy link
Owner Author

From g.rodola on November 08, 2010 07:16:53

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.

Status: Invalid

@giampaolo
Copy link
Owner Author

From siqis...@gmail.com on November 08, 2010 07:37:00

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.

@iyedsaadli
Copy link

ps aux | grep airflow
kill all airflow processes and restart with :
airflow webserver
airflow scheduler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants