python decorator that call a function with delay
why this code does not work?
from threading import Timer
def delayed(seconds):
def decorator(f):
def wrapper(*args, **kargs):
Timer(seconds, f, args, kargs)
return wrapper
return decorator
@delayed(1)
def foo():
'''this function does not return'''
print('foo')
foo()
print('dudee')
i except this result: dudee foo
i have only 开发者_如何转开发 dudee
Because you didn't start your timer try like this:
from threading import Timer
def delayed(seconds):
def decorator(f):
def wrapper(*args, **kargs):
t = Timer(seconds, f, args, kargs)
t.start()
return wrapper
return decorator
@delayed(1)
def foo():
print('foo')
foo()
print('dudee')
精彩评论