我在python中有一个线程代码,我需要找出执行功能a与再次执行相同功能之间的时间。怎么做? 我尝试使用timeit模块,但结果错误
最小代码为
def abc1 ():
process
def abc2():
process
def abc3:
process
如上所述,有许多线程化的函数,我想确定函数abc1是否在时间0执行,然后在多少时间后再次执行abc1
答案 0 :(得分:0)
如果您对装饰器和线程安全性的概念感到满意,请使用以下解决方案:
import time
import threading
# Let us define a list that stores the execution start times
exec_history = []
# Define a lock which allows safe access to this list
list_lock = threading.Lock()
# Define a decorator, which will "log" the execution of its function to the list
def log_func_call(fn):
def wrapper(*args, **kwargs):
with list_lock:
# Note down which function and when it was called.
# Alternatively, you can also log to a file or terminal, etc.
exec_history.append((fn, time.time()))
return fn(*args, **kwargs)
return wrapper
# Decorate whichever method you want to track
@log_func_call
def abc1():
pass
@log_func_call
def abc2():
pass
exec_history
将在调用任何函数时以及其开始时间进行更新。这是您要找的吗?