I want run a function asynchronously and launch a streamlit dashboard.
So far I have done the following
### file launch_dashboard.py ####
import os
import asyncio
import streamlit.bootstrap
from datetime import datetime
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'dummy.py')
args = []
async def do_some_iterations():
for i in range(10):
print(datetime.now().time())
await asyncio.sleep(1)
print('... Cool!')
async def main():
task = asyncio.create_task (do_some_iterations())
await task
asyncio.run(main())
streamlit.bootstrap.run(filename, '', args, flag_options={})
### file dummy.py ####
import streamlit as st
title = st.title("Hello title.")
I am running the launch_dashboard.py script but I have to wait for the loop to finish to get the streamlit dashboad.
I would like instead to be able to open the dashboard with the loop is running.
Does anyone know how to do this please?
There is this post that looks kinda similar but the subprocess workaround will not work for me.
The awaitable task is not run concurrently as you might be expecting. Itβs just being scheduled to run and will run like any sequential (blocking) function call. Try using loop.run_in_executor() (or concurrent.futures.ThreadPoolExecutor()) to run it in a thread. See this doc.
I havenβt tried myself, but you could wrap streamlit.bootstrap.run() in an awaitable task and run both tasks concurrently. See this doc.
Thanks but I couldnt really do it. This is how I changed my file; I am getting a RuntimeError: Cannot run the event loop while another loop is running exception. If by any change you have any ideas it will be masively apreciated!
Also, I couldnt understand the approach with executor, hence didnt do anything with thisβ¦
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.