Use an existing ioloop into streamlit

Could someone please help with this? I have some code that lets say does some lengthy iterations and I want to open a streamlit dashboard while these iterations are running. My first attempt here drops an exception: RuntimeError: Cannot run the event loop while another loop is running.
I have been trying to make a similar function to streamlit.bootstrap.run() but failed again. Here is what I have written (which obviously doesnt work). It throws a AttributeError: 'ProactorEventLoop' object has no attribute 'add_callback' error.

How can I do this please? I am really a newbie with the async python paradigm

import os
import asyncio
import streamlit.bootstrap
from streamlit.server.server import Server
from datetime import datetime

async def do_some_iterations():
    for i in range(10):
        print(datetime.now().time())
        await asyncio.sleep(1)
    print('... Cool!')


async def main():
    await asyncio.gather(
        do_some_iterations(),
        load_dashboard()
    )

def run(
    main_script_path: str,
        ioloop,
        command_line,
        args,
        flag_options
) -> None:
    streamlit.bootstrap._fix_sys_path(main_script_path)
    streamlit.bootstrap._fix_matplotlib_crash()
    streamlit.bootstrap._fix_tornado_crash()
    streamlit.bootstrap._fix_sys_argv(main_script_path, args)
    streamlit.bootstrap._fix_pydeck_mapbox_api_warning()
    streamlit.bootstrap._install_config_watchers(flag_options)
    streamlit.bootstrap._install_pages_watcher(main_script_path)

    # Install a signal handler that will shut down the ioloop
    # and close all our threads
    streamlit.bootstrap._set_up_signal_handler()


    # Create and start the server.
    server = Server(ioloop, main_script_path, command_line)
    server.start(streamlit.bootstrap._on_server_start)

    # Start the ioloop. This function will not return until the
    # server is shut down.
    # ioloop.start()


async def load_dashboard():
    print('Loading dashboard')
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, 'dummy.py')
    args = []
    loop = asyncio.get_event_loop()
    run(filename, loop, '', args, flag_options={})
    print('...something')


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.close()

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