From 70a93ba677d99dfd43ff3b1d8a13fc105086837e Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Sat, 12 Oct 2024 13:59:53 +0200 Subject: [PATCH] Use asyncio.gather (#430) * Use asyncio.gather * Use new_event_loop() in example.py --- example.py | 2 +- gios/__init__.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example.py b/example.py index 71d862c..0315dde 100644 --- a/example.py +++ b/example.py @@ -36,6 +36,6 @@ async def main() -> None: print(data) -loop = asyncio.get_event_loop() +loop = asyncio.new_event_loop() loop.run_until_complete(main()) loop.close() diff --git a/gios/__init__.py b/gios/__init__.py index 1d733ee..0146503 100644 --- a/gios/__init__.py +++ b/gios/__init__.py @@ -1,5 +1,6 @@ """Python wrapper for getting air quality data from GIOS.""" +import asyncio import logging from contextlib import suppress from http import HTTPStatus @@ -131,11 +132,10 @@ async def _get_station(self) -> Any: async def _get_all_sensors(self, sensors: dict[str, Any]) -> dict[str, Any]: """Retrieve all sensors data.""" - data: dict[str, Any] = {} - for sensor in sensors: - sensor_data = await self._get_sensor(sensors[sensor][ATTR_ID]) - data[sensor] = sensor_data - return data + results = await asyncio.gather( + *[self._get_sensor(sensors[sensor][ATTR_ID]) for sensor in sensors] + ) + return dict(zip(sensors, results, strict=True)) async def _get_sensor(self, sensor: int) -> Any: """Retrieve sensor data."""