pnpnpn / timeout-decorator

Timeout decorator for Python
MIT License
620 stars 96 forks source link

using wrapt instead of wraps, refractoring only use one wrapped function, change Exception, adding powerful eval option #41

Open bitranox opened 6 years ago

bitranox commented 6 years ago
@timeout(5)
def foo():
    interesting stuff happens here

@timeout(dec_timeout=20)
def foo2():
    foo(dec_timeout=1)      # overriding the timeout of 5 seconds defined before in the decorator
                            # of foo. I chose the name dec_timeout not to collide with other
                            # keyword parameters the function might use the parameter name  "timeout"
                            # itself - it would be a too common name ... 
                            # so dec_timeout for decorator timeout.

def foo3(x,y,z):
    print('{x}-{y}-{z}'.format(**locals()))

# use the decorator without decorating the function : 
timeout(3)(foo3)(1,2,3)
'1-2-3'

# using the dec_allow_eval parameter:
# This is very powerful, but is also very dangerous if you accept strings to evaluate from 
# UNTRUSTED input.
# read: https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html

def class Foo(object):
    def __init__(self,x):
        self.x=x

    @timeout(dec_timeout='instance.x', dec_allow_eval=True)
    def foo2(self):
        print('swallow')

    @timeout(1)
    def foo3(self):
        print('parrot')

    @timeout(dec_timeout='args[0] + kwargs.pop("max_time",0)', dec_allow_eval=True)
    def foo4(self,base_delay):
        time.sleep(base_delay)
        print('knight')

# or override via kwarg :
my_foo = Foo(3)
my_foo.foo2(dec_timeout='instance.x * 2.5 +1')
my_foo.foo3(dec_timeout='instance.x * 2.5 +1', dec_allow_eval=True)
my_foo.foo4(1,max_time=3)  # this will time out in 4 seconds
bitranox commented 6 years ago

implemented multiprocess.pipe instead of multiprocessing.queue -it is faster and can be probably used on amazon AWS

bitranox commented 6 years ago

implemented multiprocess.pipe instead of multiprocessing.queue -it is faster and can be probably used on amazon AWS