首页 > 编程语言 >Python代码片段

Python代码片段

时间:2022-11-16 11:44:47浏览次数:41  
标签:片段 idx Python 代码 datetime lst cost func def

1 记录函数执行耗时

 1 def timeToRunWithParams(level, param_idx_lst):
 2     def inner(func):
 3         def wrapper(*args, **kwargs):
 4             s_date = datetime.datetime.now()
 5             result = func(*args, **kwargs)
 6             duration = datetime.datetime.now() - s_date
 7             cost_ms = round(duration.total_seconds() * 1000, 2)
 8             if "DEBUG" == level.upper():
 9                 sub_params = [args[idx] for idx in param_idx_lst]
10                 log.info('func={} with sub_params={} cost={} milliseconds'.format(func.__name__, sub_params, cost_ms))
11             else:
12                 log.info('func={} cost={} milliseconds'.format(func.__name__, cost_ms))
13             return result
14         return wrapper
15     return inner
16 
17 @timeToRunWithParams(level="info", param_idx_lst=[])
18 def job():
19     # to do something
20     pass

 

标签:片段,idx,Python,代码,datetime,lst,cost,func,def
From: https://www.cnblogs.com/standby/p/16895356.html

相关文章