[Tutor] I may have solved my problem with killing tasks in Windows

Alan Gauld alan.gauld at btinternet.com
Sat Apr 25 23:28:16 CEST 2009

"Ian Campbell" <ian-campbell at shaw.ca> wrote 

>I am trying to use this  so maybe  no one has to reply... sorry to  
> inconvenience  anyone.

No inconvenience and I'm glad you posted the solution 
you found.

That is pretty much what I had in mind but I didn't know 
you could get the _handle from the return value in Popen!
That makes it much easier. I would still tend to run the 
execute and wait from a separate thread just in case...

> def execute_and_wait(args, timeout=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()

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list