Skip to content

Commit 28d26b6

Browse files
committed
examples: use context manager in detection_callback.py
If we don't use a context manager, then we can get the following error after a CTRL+C: Exception ignored in: <function BleakScannerWinRT.start.<locals>.<lambda> at 0x0000020C3A76EE80> Traceback (most recent call last): File "C:\Users\david\Documents\GitHub\bleak\bleak\backends\winrt\scanner.py", line 238, in <lambda> lambda s, e: event_loop.call_soon_threadsafe(self._received_handler, s, e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\david\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 840, in call_soon_threadsafe self._check_closed() File "C:\Users\david\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 541, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed
1 parent 3d2b0ec commit 28d26b6

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

examples/detection_callback.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ async def main(args: argparse.Namespace):
3030

3131
while True:
3232
logger.info("(re)starting scanner")
33-
await scanner.start()
34-
await asyncio.sleep(5.0)
35-
await scanner.stop()
33+
async with scanner:
34+
await asyncio.sleep(5.0)
3635

3736

3837
if __name__ == "__main__":

0 commit comments

Comments
 (0)