Discover not working with new event loops.
See original GitHub issue- bleak version: 0.4.3
- Python version: 3.5.3
- Operating System: Debian/Raspian-Stretch
Description
I want to write a small module that deals with a bluetoothle connection. Everything works fine until I try to use a new event loop together with bleak.discover(). Please see MWE below for details.
What I Did
import asyncio
from bleak import discover
import logging
async def printer2(loop: asyncio.AbstractEventLoop):
print("Start Printer2")
devices = await discover(loop=loop) # This function gets stuck here.
for d in devices:
print(d.name)
print("Hey")
return 42
async def printer(loop: asyncio.AbstractEventLoop): # This function works as expected
print("Start Printer")
await asyncio.sleep(5, loop=loop)
print("Hey")
return 42
def create():
loop = asyncio.new_event_loop() # replacing this with asyncio.get_event_loop() works..
asyncio.set_event_loop(loop)
return loop
logging.basicConfig(level=logging.DEBUG)
loop = create()
retval = loop.run_until_complete(printer(loop))
print(retval)
retval = loop.run_until_complete(printer2(loop))
print(retval)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
python - Discover what is blocking the event loop
This is making the program not work, as some tasks need to answer a message lets say in 5 seconds, on their network...
Read more >The Curious Case of Angular and the Infinite Change Event ...
This is the behind-the-curtain magic that makes Angular 'just work'. Today we pull back the curtain on Angular change events and discover ......
Read more >Event Loop — Python 3.11.1 documentation
Raise a RuntimeError if there is no running event loop. This function can only be called from a coroutine or a callback. New...
Read more >Introduction to the event loop in Node.js - IBM Developer
The event loop enables Node's non-blocking I/O model, which is what helps it to scale under load. Learn more about the event loop...
Read more >How JavaScript Event Loop works when executing ...
In this case, if we were working on a synchronous execution stack, the setTimeout function would cause the execution to stop 10 seconds...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The core of the problem here is that bleak uses twisted below asyncio, though the
asyncioreactor
. Creating a new loop only via asyncio will therefore not work, since it is not the loop installed in the twisted reactor. There might be a way around this, but it involves changing a lot of the code in the BlueZ backend…The first example you provided works because there is no bleak interaction in the
printer
method. It does not matter whether or not you create a new event loop. Theprinter2
however callsdiscover
with the new loop, but since it is not the one installed in the twisted reactor, you are waiting on the wrong loop for the results.The second one is similar. Once you create the new loop and set it as default, all awaiting on the Service bus in bleak will fail.
The simple solution: stop using multiple event loops with bleak’s Linux backend. The hard solution: find a nice way to have a separate reactor for each
BleakClient
anddiscover
call. However, this is not a priority for bleak right now…I will close this in the meantime. If I find the time and a solution I will reopen it.
Yeah sounds good. Maybe add this piece of information to the documentation while it’s relevant.