0

I'm trying to make a decoration on some specific functions. If the functions works more than, say five seconds, the deco will raise errors or interrupt the function.

def time_limit(func):
    def time_out():
        raise AssertionError

    @wraps(func)
    def deco(*args, **kwargs):
        timer = Timer(5, time_out)
        timer.start()
        res = func(*args, **kwargs)
        return res

    return deco

however even though the deco works, the function still works without interruptions:

In [69]: @time_limit
...: def f():
...:     time.sleep(6)
...:     print 'aaa'
...:     

In [70]: f()
Exception in thread Thread-2764:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File      "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1073, in run
self.function(*self.args, **self.kwargs)
File "<ipython-input-68-cc74c901d8b8>", line 4, in time_out
raise AssertionError
AssertionError

aaa

How to fix the problem? ps. I'm using apscheduler to circulalte the function, so a time-out decoration is better.

Kadakyo
  • 11
  • 3

0 Answers0