From 4b9e0c339a06913cb9b0a2ac37240cc640cd3d2a Mon Sep 17 00:00:00 2001 From: Tero Paloheimo Date: Mon, 11 Nov 2024 12:06:51 +0200 Subject: [PATCH] docs: fix most asyncio DeprecationWarnings 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. --- README.md | 6 +++--- examples/find_tags_async_bleak.py | 9 ++++++--- examples/get_async_bleak.py | 9 ++++++--- examples/get_first_async_bleak.py | 2 +- examples/rx_async.py | 5 +---- examples/send_updated_async.py | 3 +-- ruuvitag_sensor/__main__.py | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 436448c..7de6657 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. @@ -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. diff --git a/examples/find_tags_async_bleak.py b/examples/find_tags_async_bleak.py index 496a16c..7a60b75 100644 --- a/examples/find_tags_async_bleak.py +++ b/examples/find_tags_async_bleak.py @@ -1,5 +1,5 @@ """ -Asynchronous find RuuviTags +Asynchronously find RuuviTags """ import asyncio @@ -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()) diff --git a/examples/get_async_bleak.py b/examples/get_async_bleak.py index 49c1732..161fba0 100644 --- a/examples/get_async_bleak.py +++ b/examples/get_async_bleak.py @@ -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()) diff --git a/examples/get_first_async_bleak.py b/examples/get_first_async_bleak.py index e33af9b..ca7150e 100644 --- a/examples/get_first_async_bleak.py +++ b/examples/get_first_async_bleak.py @@ -21,4 +21,4 @@ async def main(): if __name__ == "__main__": - asyncio.get_event_loop().run_until_complete(main()) + asyncio.run(main()) diff --git a/examples/rx_async.py b/examples/rx_async.py index dd7a334..36c5cba 100644 --- a/examples/rx_async.py +++ b/examples/rx_async.py @@ -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()) diff --git a/examples/send_updated_async.py b/examples/send_updated_async.py index 6582dcd..284fc77 100644 --- a/examples/send_updated_async.py +++ b/examples/send_updated_async.py @@ -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)) diff --git a/ruuvitag_sensor/__main__.py b/ruuvitag_sensor/__main__.py index 05fa188..0621c97 100644 --- a/ruuvitag_sensor/__main__.py +++ b/ruuvitag_sensor/__main__.py @@ -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)