Skip to content

Commit 70a93ba

Browse files
authored
Use asyncio.gather (#430)
* Use asyncio.gather * Use new_event_loop() in example.py
1 parent b5ad619 commit 70a93ba

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ async def main() -> None:
3636
print(data)
3737

3838

39-
loop = asyncio.get_event_loop()
39+
loop = asyncio.new_event_loop()
4040
loop.run_until_complete(main())
4141
loop.close()

gios/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Python wrapper for getting air quality data from GIOS."""
22

3+
import asyncio
34
import logging
45
from contextlib import suppress
56
from http import HTTPStatus
@@ -131,11 +132,10 @@ async def _get_station(self) -> Any:
131132

132133
async def _get_all_sensors(self, sensors: dict[str, Any]) -> dict[str, Any]:
133134
"""Retrieve all sensors data."""
134-
data: dict[str, Any] = {}
135-
for sensor in sensors:
136-
sensor_data = await self._get_sensor(sensors[sensor][ATTR_ID])
137-
data[sensor] = sensor_data
138-
return data
135+
results = await asyncio.gather(
136+
*[self._get_sensor(sensors[sensor][ATTR_ID]) for sensor in sensors]
137+
)
138+
return dict(zip(sensors, results, strict=True))
139139

140140
async def _get_sensor(self, sensor: int) -> Any:
141141
"""Retrieve sensor data."""

0 commit comments

Comments
 (0)