Timeout a function (windows)?

前端 未结 2 1753
孤城傲影
孤城傲影 2020-11-30 06:01

I am trying to implement timeout for a particular function. I have checked many of the questions in SE and couldn\'t find any solution which fits my problem, because:

<
2条回答
  •  孤城傲影
    2020-11-30 06:33

    @acushner's answer adapted for python 3.5:

    from threading import Thread
    import functools
    
    def timeout(seconds_before_timeout):
        def deco(func):
            @functools.wraps(func)
            def wrapper(*args, **kwargs):
                res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, seconds_before_timeout))]
                def newFunc():
                    try:
                        res[0] = func(*args, **kwargs)
                    except Exception as e:
                        res[0] = e
                t = Thread(target=newFunc)
                t.daemon = True
                try:
                    t.start()
                    t.join(seconds_before_timeout)
                except Exception as e:
                    print('error starting thread')
                    raise e
                ret = res[0]
                if isinstance(ret, BaseException):
                    raise ret
                return ret
            return wrapper
        return deco
    

提交回复
热议问题