how to set timeout for os.popen
eryksun at gmail.com
Mon Apr 16 17:08:34 EDT 2018
On Mon, Apr 16, 2018 at 1:33 PM, Jugurtha Hadjar
<jugurtha.hadjar at gmail.com> wrote:
> On 04/15/2018 12:01 PM, Ho Yeung Lee wrote:
>> while 1:
>> runner = os.popen("tracert -d www.hello.com")
>> how to set timeout and know that this is timeout?
> def timeout(duration, handler):
> """Timeout after `duration` seconds."""
> signal.signal(signal.SIGALRM, handler)
The OP is most likely using Windows, which has tracert.exe instead of
Windows doesn't implement POSIX signals. The C runtime emulates the
ones required by standard C. This includes SIGSEGV, SIGFPE, SIGILL
based on OS exceptions; SIGINT (Ctrl+C) and SIGBREAK (Ctrl+Break) for
console applications; and SIGABRT and SIGTERM for use in-process via C
raise() and abort().
There's no alarm function to interrupt a thread that's waiting on
synchronous I/O. You could implement something similar using another
thread that calls CancelSynchronousIo(). Altertable waits can also be
interrupted, but code has to be designed with this in mind.
That said, the OP can simply switch to using
subprocess.check_output(command_line, timeout=TIMEOUT). subprocess
supports a timeout on Windows by joining a worker thread that reads
More information about the Python-list