Skip to content

Commit

Permalink
docs: fix most asyncio DeprecationWarnings
Browse files Browse the repository at this point in the history
Calling get_event_loop() in Python 3.12 and newer when no event loop
is running is deprecated. The recommended option is to use asyncio.run()
instead.
examples/http_server_asyncio.py is not updated as the example
is quite broken as is so changing it without fixing the existing
problems is not wise.
  • Loading branch information
terop committed Nov 11, 2024
1 parent cc8e811 commit 4b9e0c3
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def main():


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
```

The optional list of MACs can be passed to the `get_data_async` function.
Expand All @@ -111,7 +111,7 @@ async def main():


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
```

The line `if __name__ == "__main__":` is required on Windows and macOS due to the way the `multiprocessing` library works. It is not required on Linux, but it is recommended. It is omitted from the rest of the examples below.
Expand Down Expand Up @@ -468,7 +468,7 @@ async def main():


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
```

Check [get_async_bleak](https://github.com/ttu/ruuvitag-sensor/blob/master/examples/get_async_bleak.py) and other async examples from [examples](https://github.com/ttu/ruuvitag-sensor/tree/master/examples) directory.
Expand Down
9 changes: 6 additions & 3 deletions examples/find_tags_async_bleak.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Asynchronous find RuuviTags
Asynchronously find RuuviTags
"""

import asyncio
Expand All @@ -11,8 +11,11 @@


async def main():
await RuuviTagSensor.find_ruuvitags_async()
try:
await RuuviTagSensor.find_ruuvitags_async()
except asyncio.exceptions.CancelledError:
print("Scan stopped")


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
9 changes: 6 additions & 3 deletions examples/get_async_bleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@


async def main():
async for data in RuuviTagSensor.get_data_async():
print(data)
try:
async for data in RuuviTagSensor.get_data_async():
print(data)
except asyncio.exceptions.CancelledError:
print("Scan stopped")


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
2 changes: 1 addition & 1 deletion examples/get_first_async_bleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ async def main():


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
5 changes: 1 addition & 4 deletions examples/rx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ async def main():


if __name__ == "__main__":
# https://stackoverflow.com/a/56727859/1292530
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_forever()
asyncio.run(main())
3 changes: 1 addition & 2 deletions examples/send_updated_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ def handle_new_data(new_data):
executor = ProcessPoolExecutor()
executor.submit(run_get_data_background, q)

loop = asyncio.get_event_loop()
loop.run_until_complete(handle_queue(q))
asyncio.run(handle_queue(q))
2 changes: 1 addition & 1 deletion ruuvitag_sensor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ def _sync_main_handle(arguments: argparse.Namespace):
sys.exit(0)

if is_async_adapter(ruuvitag_sensor.ruuvi.ble):
asyncio.get_event_loop().run_until_complete(_async_main_handle(args))
asyncio.run(_async_main_handle(args))
else:
_sync_main_handle(args)

0 comments on commit 4b9e0c3

Please sign in to comment.