python(flask)中函数的最大执行时间

时间:2013-09-02 21:46:46

标签: python flask

我试图限制允许函数在python(flask)中运行的时间。它看起来像这样:

def my_function:
    try:
        long_function_time()
    catch TimeOutException:
        return "Function took too long to execute"

def long_function_time:
    #stuff here

我最初尝试使用信号,但被告知这不是一个好方法,因为烧瓶在线程环境中运行。我希望最大执行时间灵活,以便我可以轻松更改它。

我目前使用的代码(有时不起作用,不知道原因):

class TimedOutExc(Exception):
  pass

def deadline(timeout, *args):
  def decorate(f):
    def handler(signum, frame):
      signal.alarm(0)
      raise TimedOutExc()

    def new_f(*args):

      signal.signal(signal.SIGALRM, handler)
      signal.alarm(timeout)
      return f(*args)

    new_f.__name__ = f.__name__
    return new_f
  return decorate

提前致谢!

2 个答案:

答案 0 :(得分:5)

这适用于CPython 2(使用2.7测试),显然不使用信号,而是使用CPython解释器循环。因此,您仍然必须处理本机I / O调用中的非Python块和类似的(例如,您必须设置socket.timeout)。此外,它有一个可能很大的运行时惩罚(你可以让它检查每个* n *次调用超时,以减轻一点)。

仍然有助于解决某类问题(例如计算)。如果您希望它与分析器共存,您将不得不投入一些工作。

import sys
import time

class WatchdogTimeoutError(RuntimeError):
    """Raised in case of runtime limit violations."""

def sleeper(tick):
    """Endless loop."""
    while True:
        time.sleep(tick)

def watchdog(timeout, code, *args, **kwargs):
    "Time-limited execution."
    def tracer(frame, event, arg, start=time.time()):
        "Helper."
        now = time.time()
        if now > start + timeout:
            raise WatchdogTimeoutError(start, now)
        return tracer if event == "call" else None

    old_tracer = sys.gettrace()
    try:
        sys.settrace(tracer)
        code(*args, **kwargs)
    finally:
        sys.settrace(old_tracer)

def demo():
    """Show timeout executor."""
    try:
        watchdog(5, sleeper, 0.1)
    except WatchdogTimeoutError, exc:
        start, abort = exc.args
        print "Aborted after %.3f secs" % (abort - start,)
    else:
        print "Ended"

if __name__ == "__main__":
    demo()

答案 1 :(得分:3)

尝试使用线程:

from multiprocessing import Pool, TimeoutError
from time import sleep

class TimedOutExc(Exception):
  pass

def f(x):
    sleep(2)
    return x*x

pool = Pool(processes=1)
result = pool.apply_async(f, (1,))
try:
    print(result.get(timeout=1))
except TimeoutError:
    print 'timeout'
    raise TimedOutExc()