periodic task using asyncio

This commit is contained in:
Jörn-Michael Miehe 2023-08-31 22:55:31 +00:00
parent 862e78eec0
commit 8f6a7a37fd
3 changed files with 23 additions and 5 deletions

1
.vscode/launch.json vendored
View file

@ -10,6 +10,7 @@
"request": "launch",
"module": "kiwi_simple_metrics.main",
"env": {
"METRIC__INTERVAL": "5",
"METRIC__DISK__PATHS": "[\"/var\", \"/\", \"/dev\"]",
},
"justMyCode": true

View file

@ -1,14 +1,28 @@
#!/usr/bin/python3
import asyncio
from . import metrics
from .settings import SETTINGS
async def run_metrics() -> None:
while True:
interval = asyncio.sleep(SETTINGS.interval)
print(metrics.Report.concat(
metrics.cpu(),
metrics.memory(),
metrics.disk(),
))
await interval
def main() -> None:
print(metrics.Report.concat(
metrics.cpu(),
metrics.memory(),
metrics.disk(),
))
loop = asyncio.get_event_loop()
loop.create_task(run_metrics())
loop.run_forever()
if __name__ == "__main__":

View file

@ -88,6 +88,9 @@ class Settings(BaseSettings):
env_nested_delimiter="__",
)
# time between gathering reports
interval: float = 600
# separates metrics and values in reports
separator: str = ", "