Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91088 > unrolled thread

Re: decorators and alarms

Started byJason Friedman <jsf80238@gmail.com>
First post2015-05-22 21:20 -0600
Last post2015-05-22 20:43 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: decorators and alarms Jason Friedman <jsf80238@gmail.com> - 2015-05-22 21:20 -0600
    Re: decorators and alarms Paul Rubin <no.email@nospam.invalid> - 2015-05-22 20:43 -0700

#91088 — Re: decorators and alarms

FromJason Friedman <jsf80238@gmail.com>
Date2015-05-22 21:20 -0600
SubjectRe: decorators and alarms
Message-ID<mailman.250.1432351246.17265.python-list@python.org>
> But, I'd like to expand this to take some generic code, not just a
> shell command, and terminate it if it does not return quickly.
>
> @time_limiter
> def generic_function(arg1, arg2, time_limit=10):
>     do_some_stuff()
>     do_some_other_stuff()
>     return val1, val2
>
> If generic_function does not finish within 10 seconds it would stop
> executing and throw an exception.

Found an answer:
https://wiki.python.org/moin/PythonDecoratorLibrary#Function_Timeout

[toc] | [next] | [standalone]


#91091

FromPaul Rubin <no.email@nospam.invalid>
Date2015-05-22 20:43 -0700
Message-ID<87zj4wx8ka.fsf@jester.gateway.sonic.net>
In reply to#91088
Jason Friedman <jsf80238@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web