concurrent metrics working

This commit is contained in:
Jörn-Michael Miehe 2023-09-02 02:38:12 +00:00
parent 32231f1b2d
commit 4f67c6158d
2 changed files with 23 additions and 11 deletions

View file

@ -11,19 +11,26 @@ def run_metrics(
executor: concurrent.futures.Executor, executor: concurrent.futures.Executor,
*_metrics: metrics.Metric, *_metrics: metrics.Metric,
) -> None: ) -> None:
# === conc experiment === # run metrics in executor
tasks = [
executor.submit(metric)
for metric in _metrics
]
futures = concurrent.futures.wait( # wait for finish
executor.submit(metric) for metric in _metrics # pair up each result with its task index
).done results = (
(tasks.index(future), future.result())
for future in concurrent.futures.wait(tasks).done
)
print(list(future.result() for future in futures)) # extract reports in task index order
reports = (
report
for _, report in sorted(results, key=lambda x: x[0])
)
# === end conc === # create summary report
reports = (metric() for metric in _metrics)
# create single report from metrics
report = metrics.Report.summary(*reports) report = metrics.Report.summary(*reports)
# maybe print this to stdout # maybe print this to stdout
@ -38,7 +45,9 @@ async def async_main_loop() -> None:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
while True: while True:
with concurrent.futures.ThreadPoolExecutor() as pool: with concurrent.futures.ThreadPoolExecutor(
max_workers=SETTINGS.threads,
) as pool:
await asyncio.gather( await asyncio.gather(
asyncio.sleep(SETTINGS.interval), asyncio.sleep(SETTINGS.interval),
loop.run_in_executor( loop.run_in_executor(

View file

@ -19,6 +19,9 @@ class Settings(BaseSettings):
# separates metrics and values in reports # separates metrics and values in reports
separator: str = ", " separator: str = ", "
# maximum threads for concurrent metric execution
threads: int | None = None
# metrics settings # metrics settings
cpu: metric.CpuMS = metric.CpuMS() cpu: metric.CpuMS = metric.CpuMS()
memory: metric.MemoryMS = metric.MemoryMS() memory: metric.MemoryMS = metric.MemoryMS()