On Mon, Apr 16, 2018 at 1:33 PM, Jugurtha Hadjar
<jugurtha.had...@gmail.com> wrote:
> On 04/15/2018 12:01 PM, Ho Yeung Lee wrote:
>> while 1:
>> runner = os.popen("tracert -d www.hello.com")
>> o=runner.read()
>> how to set timeout and know that this is timeout?
> @contextmanager
> def timeout(duration, handler):
>     """Timeout after `duration` seconds."""
>     signal.signal(signal.SIGALRM, handler)
>     signal.alarm(duration)
>     try:
>         yield
>     finally:
>         signal.alarm(0)

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
from stdout.

Reply via email to