python 定时器装饰器,来自Fluent Python的源代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 定时器装饰器,来自Fluent Python的源代码相关的知识,希望对你有一定的参考价值。

import time
import functools


def clock(func):
    @functools.wraps(func)
    def clocked(*args, **kwds):
        t0 = time.time()
        result = func(*args, **kwds)
        elapsed = time.time() - t0
        name = func.__name__
        arg_lst = []
        if args:
            arg_lst.append(', '.join(repr(arg) for arg in args))
        if kwds:
            pairs = ['%s=%r' % (key, value) for key, value in sorted(kwds.items())]
            arg_lst.append(', '.join(pairs))
        arg_str = ', '.join(arg_lst)
        print('[%0.8fs] %s(%s) -> %r ' % (elapsed, name, arg_str, result))
        return result
    return clocked


@clock
def hello(a, b, c, *, d, e, f):
    time.sleep(1)

hello(1, 'L3nvy', False, d="D", e="E", f="F")

以上是关于python 定时器装饰器,来自Fluent Python的源代码的主要内容,如果未能解决你的问题,请参考以下文章

FastAPI利用装饰器实现定时任务

FastAPI利用装饰器实现定时任务

FastAPI利用装饰器实现定时任务

python 具有定时的键过期的python缓存装饰器的示例。

假装优雅地实现定时缓存装饰器

python-装饰器