MINOR: Fix 'calculate_execution_time_generator' (#14987)

* Refactor the calculate_execution_time for generators decorator in order to fix it

* Add small comment in the code
This commit is contained in:
IceS2 2024-02-02 14:23:09 +01:00 committed by GitHub
parent 2eda75b78b
commit c2bb1e5004
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -125,12 +125,27 @@ def calculate_execution_time_generator(func):
"""
def calculate_debug_time(*args, **kwargs):
start = perf_counter()
yield from func(*args, **kwargs)
end = perf_counter()
logger.debug(
f"{func.__name__} executed in { pretty_print_time_duration(end - start)}"
)
# NOTE: We are basically implementing by hand a simplified version of 'yield from'
# in order to be able to calculate the time difference correctly.
# The 'while True' loop allows us to guarantee we are iterating over all thje values
# from func(*args, **kwargs).
generator = func(*args, **kwargs)
while True:
start = perf_counter()
try:
element = next(generator)
except StopIteration:
return
end = perf_counter()
logger.debug(
f"{func.__name__} executed in { pretty_print_time_duration(end - start)}"
)
yield element
return calculate_debug_time