Skip to content

BleakDBusError 'Operation already in progress' - is it possible to stop the BleakScanner after a timeout #1629

Closed Answered by ianchanning
ianchanning asked this question in Q&A
Discussion options

You must be logged in to vote

As a final follow up to this, the async.timeout didn't solve the problem. What did, and I took inspiration from the Bleak source code is to switch the order of the context blocks. So that the async with asyncio.timeout is performed inside the async with BleakScanner context block.

bleak/bleak/__init__.py

Lines 444 to 451 in e01e264

async with cls(**kwargs) as scanner:
try:
async with async_timeout(timeout):
async for bd, ad in scanner.advertisement_data():
if filterfunc(bd, ad):
return bd
except asyncio.TimeoutError:
return None

This was my final source code and I haven't had a problem since. Note the extra attempts to stop the scanning…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@ianchanning
Comment options

@ianchanning
Comment options

@ianchanning
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by ianchanning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1628 on August 14, 2024 13:59.