, .
RuntimeError: There is no current event loop in thread 'Thread-X'. - Bleak.
: , .
, :
class BLE:
def __init__(self):
self.loop = asyncio.get_event_loop()
def list_bluetooth_low_energy(self) -> list:
async def run() -> list:
BLElist = []
devices = await bleak.discover()
for d in devices:
BLElist.append(d.name)
return 'success', BLElist
return self.loop.run_until_complete(run())
:
ble = path.to.lib.BLE()
list = ble.list_bluetooth_low_energy()
:
. , , import , :
import asyncio, platform
from bleak import discover
def listBLE() -> dict:
async def run() -> dict:
loop = asyncio.get_event_loop()
ble_list = loop.run_until_complete(run())
return ble_list
, - , , get_event_loop():
loop = asyncio.new_event_loop()
loop = asyncio.set_event_loop()
, .
But does not answer. And my code relied on a timeout to return some values, so that was pretty bad for my application.
It took me almost two hours to figure out what the problem was import, and here is my (working) code:
def list() -> dict:
import asyncio, platform
from bleak import discover
async def run() -> dict:
loop = asyncio.get_event_loop()
ble_list = loop.run_until_complete(run())
return ble_list
source
share