在多处理功能上超时装饰器

blu*_*fer 6 python signals timeout multiprocessing

我直接从网上找到的一个例子中得到了这个装饰器:

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

Eri*_*got 9

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

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

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

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, frame):
            raise TimedOutExc()

        def new_f(*args, **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
Run Code Online (Sandbox Code Playgroud)