[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] run_tests.py subprocess mode and Build Page extensions



If you are asking if there are situations where the kill function will fail, the only one I can think of is if it is given an invalid pid. For the unit tests I cannot see access rights becoming an issue as these are child processes. Even terminating a process that has already quit doesn't raise an exception.

>>> import win32api as w
>>> import subprocess as sp
>>> o = open('stdout.txt', 'w')
>>> s =  sp.Popen('''python -c "print 'Hello.'"''', stdout=o)
>>> s.wait()
0
>>> s.pid
-468795
>>> print open('stdout.txt').read()
Hello.

>>> h = w.OpenProcess(1, 0, s.pid)
>>> w.TerminateProcess(h, 0)
>>> w.TerminateProcess(h, 0)
>>> w.CloseHandle(h)
>>> w.TerminateProcess(h, 0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
pywintypes.error: (6, 'TerminateProcess', 'The handle is invalid.')


Is is probably pointless to try and kill a process more than once on Windows. In this case it is best to let any exceptions in kill() propagate. It would indicate a bug in the program. But I cannot speak for Unix.

As for this possible kill code:

       def kill(self):
           ......
               # This works as well
               # win32api.TerminateProcess(int(self._handle), 0)
                                          # handle,  # exit code

Using the undocumented _handle attribute should be fine in this case because it is accessed within a subclass and in the unlikely case it goes the code can be easily changed. Interestingly Python 2.6 introduces the Popen terminate method as well as a Unix only kill method. Neither has a return value.

Lenard


Nicholas Dudfield wrote:

Lenard,

I will commit your change, thanks.

I must admit I took that recipe and used it without knowing exactly how it worked much the same way I would use a library.

How does the async Popen recipe look to you? I'm starting to wonder at the wisdom of using recipes. That was from ActiveState Python CookBook.

I put in one test for it but......

if ret_code is None:
   proc.kill()

Should we make it try a few times upon failure? If it fails would there be much chance of killing it in the immediate future? What situations would cause a failure?

It might even make sense to move that kill functionality into the async_sub.Popen method read_async().

read_async(time_out=10, kill=True)

Ciao.
I tried the updated run_tests and it works. Killed python interpreters disappear. Thanks for the changes.

I do suggest some modifications to Popen.kill though. First, the win32api functions raise Python exceptions on Windows errors rather than use return values. Second, handles should be closed. Here is my version:

       def kill(self):
           """kill function for Win32"""
           try:
               handle = win32api.OpenProcess(1, 0, self.pid)
               try:
                   win32api.TerminateProcess(handle, 0)
               finally:
                   win32api.CloseHandle(handle)
           except win32api.error:
               return False
           return True