I am trying to get subprocess output (on Windows) at the time the TimeoutExpired exception is raised. Any ideas?
try:
proc = subprocess.run(cmd,timeout=3)
except subprocess.TimeoutExpired:
print(???)
You need to use Popen and subprocess.PIPE in order to catch the process output when timeout expires. In particular Popen.communicate is what you need. Here is an example
proc = subprocess.Popen(["ping", "192.168.1.1"],
stdout=subprocess.PIPE)
try:
output, error = proc.communicate(timeout=2)
except subprocess.TimeoutExpired:
proc.kill()
output, error = proc.communicate()
print(output)
print(error)
This will print the process output up to the time out expiration.
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