微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 在多处理函数上超时装饰器

我直接从网上找到的一个例子中得到了这个装饰器:
class TimedOutExc(Exception):
    pass


def timeout(timeout):
    def decorate(f):
        def handler(signum,frame):
            raise TimedOutExc()

        def new_f(*args,**kwargs):

            old = signal.signal(signal.SIgalRM,handler)
            signal.alarm(timeout)

            try:
                result = f(*args,**kwargs)
            except TimedOutExc:
                return None
            finally:
                signal.signal(signal.SIgalRM,old)
            signal.alarm(0)
            return result

        new_f.func_name = f.func_name
        return new_f

    return decorate

如果f函数超时,它会抛出异常.

好吧,它可以工作但是当我在多处理函数上使用这个装饰器并且由于超时而停止时,它不会终止计算中涉及的过程.我怎样才能做到这一点?

我不想启动异常并停止程序.基本上我想要的是当f超时时,让它返回None然后终止所涉及的过程.

解决方法

虽然我同意Aaron回答的要点,但我想详细说明一下.

必须在要装饰的函数中停止多处理启动的进程;我不认为这可以通常从装饰器本身完成(装饰函数是唯一知道它启动了什么计算的实体).

您可以捕获自定义的TimedOutExc异常,而不是使装饰函数捕获SIgalARM,这可能更灵活.那么你的例子将成为:

class TimedOutExc(Exception):
    """
    Raised when a timeout happens
    """

def timeout(timeout):
    """
    Return a decorator that raises a TimedOutExc exception
    after timeout seconds,if the decorated function did not return.
    """

    def decorate(f):

        def handler(signum,**kwargs):

            old_handler = signal.signal(signal.SIgalRM,handler)
            signal.alarm(timeout)

            result = f(*args,**kwargs)  # f() always returns,in this scheme

            signal.signal(signal.SIgalRM,old_handler)  # Old signal handler is restored
            signal.alarm(0)  # Alarm removed

            return result

        new_f.func_name = f.func_name
        return new_f

    return decorate

@timeout(10)
def function_that_takes_a_long_time():
    try:
        # ... long,parallel calculation ...
    except TimedOutExc:
        # ... Code that shuts down the processes ...
        # ...
        return None  # Or exception raised,which means that the calculation is not complete

原文地址:https://www.jb51.cc/python/186103.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐