decorators and alarms

Paul Rubin no.email at nospam.invalid
Fri May 22 23:43:33 EDT 2015


Jason Friedman <jsf80238 at gmail.com> writes:
> Found an answer:
> https://wiki.python.org/moin/PythonDecoratorLibrary#Function_Timeout

That is pretty limited since signal handlers always run in the main
thread, and that wrapper takes over the sigalarm handler which some
other part of the program might also want to use.

In general what you're asking for is a hassle in Python.  In the limited
case, using sigalarm like that can be ok.  In some other cases there's a
hack involving the C API that lets you kill a thread asynchronously (I
haven't had to use that so far).  Some other times (you're waiting on
socket i/o rather than computation), you can run the i/o operation in a
new thread, sleep til the timeout, then return an error code, arranging
to ignore the i/o op when it finally finishes or times out, so you leave
a thread lingering around for a while after you've given up on it (I've
done that).  You could use async i/o and track everything yourself (I
haven't done that except slightly).  You could use subprocesses and
actually kill them on timeout (I've done that).  Erlang has its act
together better than Python does for this sort of thing.



More information about the Python-list mailing list