Skip to content

Instantly share code, notes, and snippets.

@workze
Last active April 22, 2020 01:59
Show Gist options
  • Save workze/bc52e72943b2168d8982562ee36405d0 to your computer and use it in GitHub Desktop.
Save workze/bc52e72943b2168d8982562ee36405d0 to your computer and use it in GitHub Desktop.
import time
import functools
def timer(timer_id):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s():' % (timer_id, func.__name__))
start_time = time.time()
result = func(*args, **kw)
end_time = time.time()
spent_time = end_time - start_time
# 记录时间
print(timer_id, 'spend', spent_time)
return result
return wrapper
return decorator
class A():
@timer('metric.timer.aau.receive.image')
def do(self):
time.sleep(1)
if __name__ == '__main__':
a = A()
a.do()
=============================
def feature(*ids):
def feature_wrapper(f):
def wrapper(*args, **kwargs):
f(*args, **kwargs)
return wrapper
return feature_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment