Calling bleak's BLE scan from synchronous code freezes the entire UI. Once you bridge it through an asyncio event loop, the UI stays responsive while scanning and connecting.
The BLE dashboard I built needs to connect to devices and read/write GATT. bleak is the go-to Python BLE library, but it's asynchronous by design:
BleakScanner.discover() # coroutine
BleakClient.connect() # coroutine
These functions return a "coroutine object," not a result. If you call them directly the usual synchronous way, one of two things happens: either there's no event loop to run them, or the whole flow blocks and waits there — and the UI stops responding.
The reason isn't hard to see: a BLE scan is fundamentally "waiting" — waiting for a device to advertise, for a connection to establish, for a GATT response. Synchronous code can't do anything else while it waits, so the screen simply freezes.
asyncio fits I/O-bound work (waiting on the network, files, Bluetooth). It works by using a single event loop to schedule tasks: while one task is waiting on I/O, the loop goes off to handle something else.
To run a coroutine from synchronous code, there are only three steps:
import asyncio
from bleak import BleakScanner
# ① Create a new event loop
loop = asyncio.new_event_loop()
# ② Set it as the current thread's event loop
asyncio.set_event_loop(loop)
# ③ Run the coroutine until it completes
devices = loop.run_until_complete(BleakScanner.discover(timeout=5.0))
Wrap it in a function, with resource cleanup:
def scan_ble_devices():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(
BleakScanner.discover(timeout=5.0)
)
finally:
loop.close()
Connecting and reading a GATT characteristic follows the same pattern:
async def read_gatt(address, char_uuid):
async with BleakClient(address) as client:
if client.is_connected:
return await client.read_gatt_char(char_uuid)
def do_read(address, char_uuid):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(read_gatt(address, char_uuid))
finally:
loop.close()
| Function | Purpose |
|---|---|
new_event_loop() | Creates a new event loop (the scheduler for async tasks) |
set_event_loop() | Sets it as the default loop for the current thread |
run_until_complete() | Runs a coroutine to completion — the bridge between sync and async |
run_until_complete() is itself blocking. That's fine inside a Flask request-handling thread, but if you call it directly on the GUI main thread the screen still freezes — in that case you need to move the whole thing onto a separate thread.close()d, it can't be used again; you need a fresh new_event_loop().set_event_loop() again overwrites the previous one.import threading
def run_async_in_thread(coro):
def worker():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(coro)
finally:
loop.close()
threading.Thread(target=worker, daemon=True).start()
After switching to the event-loop bridge, the UI stays usable while BLE scanning and connecting, and the scan results come back correctly. Flask handles each request on its own thread, so using this pattern directly inside a route works.
asyncio.gather() to run them concurrently instead of waiting one by one.new_event_loop() → set_event_loop() → run_until_complete().run_until_complete() blocks: it's fine in a request thread, but on the GUI main thread you need a separate thread.