开发者

python decorator that call a function with delay

开发者 https://www.devze.com 2023-02-15 04:46 出处:网络
why this code does not work? from threading import Timer def delayed(seconds): def decorator(f): def wrapper(*args, **kargs):

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

精彩评论

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