Need advice on running asyncio in streamlit


I’m writing a simple code to list nearby bluetooth devices.

import streamlit as st
import asyncio
from bleak import BleakClient, BleakScanner

async def connect_to_device():
    scanner = BleakScanner()
    devices = await
    for d in devices:

At the first run, it works: it writes the list of the devices.
but when I refresh the page, it shows the error - “RuntimeError: Event loop is closed”

2022-08-23 10:25:47.026 Uncaught app exception
Traceback (most recent call last):
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/", line 556, in _run_script
    exec(code, module.__dict__)
  File "/home/mulkkyul/22_BLE/python/", line 12, in <module>
  File "/usr/lib/python3.8/asyncio/", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/", line 608, in run_until_complete
    return future.result()
  File "/home/mulkkyul/22_BLE/python/", line 7, in connect_to_device
    devices = await
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/", line 116, in discover
    async with cls(**kwargs) as scanner:
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/", line 96, in __aenter__
    await self.start()
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/bluezdbus/", line 137, in start
    self._stop = await manager.active_scan(
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/bluezdbus/", line 384, in active_scan
    reply = await
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/", line 303, in call
    self._call(msg, reply_handler)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/", line 588, in _call
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/", line 326, in send
    self._writer.schedule_write(msg, future)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/", line 85, in schedule_write
    self.loop.add_writer(self.fd, self.write_callback)
  File "/usr/lib/python3.8/asyncio/", line 337, in add_writer
    return self._add_writer(fd, callback, *args)
  File "/usr/lib/python3.8/asyncio/", line 287, in _add_writer
  File "/usr/lib/python3.8/asyncio/", line 504, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

I don’t think this is the “streamlit issue”, but has anyone run into the same issue?
Any pointers would be appreciated.
Thank you!

Python 3.8.0
Streamlit, version 1.12.0
Ubuntu 18.04

Sounds like a cool project, I would like to see it when it works. I’m not very familiar with asyncio but is this something you can try?

@hack-r Thank you for the pointer!

using custom executor didn’t work for me.

For now, I’m going with this workaround - Invoking a Python subprocess as mentioned in here

import streamlit as st
import subprocess
import sys
import pandas as pd

if st.button("CHECK RSSI"):[f"{sys.executable}", ""])
    df_result = pd.read_csv("./result_rssi.csv")

it’s a bit messy but i don’t get asyncio errors :slight_smile:

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.