Skip to content

Commit

Permalink
Use asyncio.gather (#430)
Browse files Browse the repository at this point in the history
* Use asyncio.gather

* Use new_event_loop() in example.py
  • Loading branch information
bieniu authored Oct 12, 2024
1 parent b5ad619 commit 70a93ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 5 additions & 5 deletions gios/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 70a93ba

Please sign in to comment.