I’m running Debian 10.8 with Python 3.7.3 and running a subprocess inside a script which I would like to interrupt after some seconds. This works for cmd_1 but not for cmd_2 in the example below (timeout is never triggered):
import subprocess
import os, sys
# collect proxies and verify
try:
# cmd_1 = "while true; do sleep 1; done"
cmd_2 = "proxybroker find --types HTTPS -l 1000 --outfile proxybroker.txt"
timeout_sec = 3
subprocess.check_output(cmd_2, shell=True, timeout=timeout_sec)
except Exception as e:
print(e)
If I run cmd_2 in a bash, it works fine.
I did install the proxybroker package with pip3 install proxybroker
.
On another system running Ubuntu 21.04 the timeout works for both commands. Thanks if someone can provide a hint.
2
Answers
As a workaround, timeout as shell command can be used:
I can’t precisely explain why this happens, but getting rid of the gratuitous shell fixes it.
I notice that your original attempt does print the timeout message as part of the traceback when I interrupt it with a KeyboardInterrupt.