I've started a subprocess using:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate()[0]
Sometimes the command cmd hangs so my Python script also hangs at this point.
I'd like to let this run for a time (10 seconds?) and if I don't get a response, then simply kill the process and continue on with my script.
How can I do this?
If you're using python 3, Popen.communicate has a timeout kwarg:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate(timeout=10)[0]
From subprocess documentation proc.terminate() is what you 're looking for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With