From 32231f1b2d1455b869550714541209c27a01e978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:14:40 +0000 Subject: [PATCH] concurrent metrics --- kiwi_simple_metrics/main.py | 42 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/kiwi_simple_metrics/main.py b/kiwi_simple_metrics/main.py index 18b2656..7140230 100644 --- a/kiwi_simple_metrics/main.py +++ b/kiwi_simple_metrics/main.py @@ -1,12 +1,26 @@ #!/usr/bin/python3 import asyncio +import concurrent.futures from . import metrics from .settings import SETTINGS -def run_metrics(*_metrics: metrics.Metric) -> None: +def run_metrics( + executor: concurrent.futures.Executor, + *_metrics: metrics.Metric, +) -> None: + # === conc experiment === + + futures = concurrent.futures.wait( + executor.submit(metric) for metric in _metrics + ).done + + print(list(future.result() for future in futures)) + + # === end conc === + reports = (metric() for metric in _metrics) # create single report from metrics @@ -20,24 +34,26 @@ def run_metrics(*_metrics: metrics.Metric) -> None: report.push_webhook() -async def async_main() -> None: +async def async_main_loop() -> None: loop = asyncio.get_running_loop() while True: - await asyncio.gather( - asyncio.sleep(SETTINGS.interval), - loop.run_in_executor( - None, run_metrics, - metrics.cpu, - metrics.memory, - metrics.disk, - metrics.external, - ), - ) + with concurrent.futures.ThreadPoolExecutor() as pool: + await asyncio.gather( + asyncio.sleep(SETTINGS.interval), + loop.run_in_executor( + None, run_metrics, + pool, + metrics.cpu, + metrics.memory, + metrics.disk, + metrics.external, + ), + ) def main() -> None: - asyncio.run(async_main()) + asyncio.run(async_main_loop()) if __name__ == "__main__":