cross platform alternative for signal.SIGALRM?
Terry Reedy
tjreedy at udel.edu
Wed Nov 11 20:37:34 EST 2015
On 11/11/2015 11:16 AM, Ulli Horlacher wrote:
> I am rewriting a Perl program into Python (2.7).
I recommend using 3.4+ if you possibly can.
> It must run on Linux and Windows.
> With Linux I have no problems, but Windows... :-(
>
> The current show stopper is signal.SIGALRM which is not available on
> Windows:
> Perl for Windows has had SIGALRM support (or some kind of emulation).
>
> Ok, I have to redesign this part of my code:
>
> def timeout_handler(sig,frame):
> raise ValueError("timeout!")
>
> signal.signal(signal.SIGALRM,timeout_handler)
>
> while True:
> chunk = fileo.read(bs)
> sock.sendall(chunk)
> (...)
>
> What is the best practise for a cross platform timeout handler?
The cross-platform 3.4 asyncio module has some functions with timeouts.
(3.5 has new 'async' syntac which supposedly makes it easier to use. I
have not looked at this yet.)
For instance: coroutine asyncio.wait(futures, *, loop=None,
timeout=None, return_when=ALL_COMPLETED)
Wait for the Futures and coroutine objects given by the sequence
futures to complete. Coroutines will be wrapped in Tasks. Returns two
sets of Future: (done, pending).
...
Usage:
done, pending = yield from asyncio.wait(fs)
I believe the backport on pypi.python.org, called tulip, works on 2.7.
In the example above, the read/send would be a task. Wait on the task,
and when it returns, cancel the task if in pending.
--
Terry Jan Reedy
More information about the Python-list
mailing list