Set timeout and kill external windows

gagsl-py2 at yahoo.com.ar gagsl-py2 at yahoo.com.ar
Tue Jun 19 11:42:49 EDT 2007


At Saturday 16/06/2007 02:24, you wrote:

> Thanks for your reply. I am new to Python and I
cannot
> seem to figure this out. I searched for examples
based
> on your recommendation below but couldn't do it.  I
am
> not familiar with subprocess and
WaitForSingleObject.
> And I want to do this in Windows. Could you please
> write me the few lines I need to do this?

(Please keep posting on the list - other people may
help too, and I don't read this email too often).
Try this. (I had to use a private Popen attribute,
_handle).


import subprocess
from win32event import WaitForSingleObject,
WAIT_TIMEOUT
from win32api import TerminateProcess

def execute_and_wait(args, timeout=None):
    """Execute a command and wait until termination.
    If timeout elapses and still not finished, kill
it.
    args: list containing program and arguments, or a
single string.
    timeout: maximum time to wait, in ms, or None.
    """

    p = subprocess.Popen(args)
    if timeout:
        ret = WaitForSingleObject(p._handle, timeout)
        if ret==WAIT_TIMEOUT:
            TerminateProcess(p._handle, 1)
            return None
    return p.wait()

ret = execute_and_wait(["notepad",
"c:\\python\\LICENSE.txt"], 5000)
print "ret=",ret


-- 
Gabriel Genellina




	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list