开发者

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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜