Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix asyncio DeprecationWarnings #257

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 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,10 +111,12 @@ 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.
ttu marked this conversation as resolved.
Show resolved Hide resolved
Due to limitations in the RuuviTag package's Bleak adapter, Python 3.10 or later is required to use `asyncio.run(main())`.
If using Python 3.9, replace `asyncio.run(main())` with `asyncio.get_event_loop().run_until_complete(main())`.

### 2. Get sensor data synchronously with callback

Expand Down Expand Up @@ -468,7 +470,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())
11 changes: 8 additions & 3 deletions examples/get_async_bleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@


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())
# Note: Python 3.10 or later is required with asyncio.run
# For older versions of Python, use asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
ttu marked this conversation as resolved.
Show resolved Hide resolved
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())
11 changes: 7 additions & 4 deletions examples/rx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ async def main():
subject = ruuvi_rx.get_subject()
subject.subscribe(print)

ttu marked this conversation as resolved.
Show resolved Hide resolved
# keep coroutine alive
try:
await asyncio.get_running_loop().create_future()
finally:
ruuvi_rx.stop()


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))
ttu marked this conversation as resolved.
Show resolved Hide resolved
else:
_sync_main_handle(args)
2 changes: 1 addition & 1 deletion ruuvitag_sensor/ruuvi_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(self, macs: List[str] = [], bt_device: str = ""):
# Start background process

if is_async_adapter(ble):
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
loop.create_task(_run_get_data_background_async(macs, q, self._shared_data, bt_device))
else:
executor = ProcessPoolExecutor(1)
Expand Down
Loading