I need to do the following in Python. I want to spawn a process (subprocess module?), and:
- if the process ends normally, to continue exactly from the moment it terminates;
- if, otherwise, the process “gets stuck” and doesn’t terminate within (say) one hour, to kill it and continue (possibly giving it another try, in a loop).
What is the most elegant way to accomplish this?
The
subprocessmodule will be your friend. Start the process to get aPopenobject, then pass it to a function like this. Note that this only raises exception on timeout. If desired you can catch the exception and call thekill()method on thePopenprocess. (kill is new in Python 2.6, btw)