Set timeout and kill external windows program
gagsl-py2 at yahoo.com.ar
Tue Jun 12 06:26:07 CEST 2007
En Tue, 12 Jun 2007 00:32:12 -0300, Chico Jayanthan <jayanthanc at yahoo.com>
> I call an external windows program (.exe) from a
> python script. Something like os.system('test.exe'). I
> want to set a timeout for the external windows program
> to return. If no results are produced by the external
> windows program (test.ext) within this timeout limit,
> I want to terminate the external windows program and
> continue with the python script.
> How do I set the timeout and terminiate the external
> windows program from within the Python script?
Instead of os.system, use the subprocess module; you will need the pid
attribute from the returned Popen object.
Use WaitForSingleObject to wait until the process finishes, or timeout
elapses. If it has not finished yet, kill it using TerminateProcess. You
can invoke both functions using the pywin32 extensions (available from
sourceforge) or the ctypes module.
More information about the Python-list