Better throttling

This commit is contained in:
hroff-1902 2020-02-21 03:37:38 +03:00
parent 78ee36a8c6
commit 04aa74e5ad

View File

@ -108,12 +108,13 @@ class Worker:
:return: Any :return: Any
""" """
start = time.time() start = time.time()
result = func(*args, **kwargs)
logger.debug("========================================") logger.debug("========================================")
end = time.time() result = func(*args, **kwargs)
duration = max(min_secs - (end - start), 0.0) time_passed = time.time() - start
logger.debug(f"Throttling {func.__name__} for {duration:.2f} seconds") sleep_duration = max(min_secs - time_passed, 0.0)
time.sleep(duration) logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, "
f"last iteration took {time_passed:.2f} s.")
time.sleep(sleep_duration)
return result return result
def _process_stopped(self) -> None: def _process_stopped(self) -> None: