Need advice on running asyncio in streamlit

Hello!

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 scanner.discover(timeout=1.0)
    for d in devices:
        print(d)
        st.write(d)

asyncio.run(connect_to_device())

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/script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
  File "/home/mulkkyul/22_BLE/python/st_00_home.py", line 12, in <module>
    asyncio.run(connect_to_device())
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "/home/mulkkyul/22_BLE/python/st_00_home.py", line 7, in connect_to_device
    devices = await scanner.discover(timeout=1.0)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/scanner.py", line 116, in discover
    async with cls(**kwargs) as scanner:
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/scanner.py", line 96, in __aenter__
    await self.start()
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/bluezdbus/scanner.py", line 137, in start
    self._stop = await manager.active_scan(
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/bleak/backends/bluezdbus/manager.py", line 384, in active_scan
    reply = await self._bus.call(
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/message_bus.py", line 303, in call
    self._call(msg, reply_handler)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/message_bus.py", line 588, in _call
    self.send(msg)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/message_bus.py", line 326, in send
    self._writer.schedule_write(msg, future)
  File "/home/mulkkyul/22_BLE/venv/lib/python3.8/site-packages/dbus_next/aio/message_bus.py", line 85, in schedule_write
    self.loop.add_writer(self.fd, self.write_callback)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 337, in add_writer
    return self._add_writer(fd, callback, *args)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 287, in _add_writer
    self._check_closed()
  File "/usr/lib/python3.8/asyncio/base_events.py", 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"):
    subprocess.run([f"{sys.executable}", "util_checkRSSI.py"])
    df_result = pd.read_csv("./result_rssi.csv")
    st.dataframe(df_result)

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.