I’m working on a Streamlit app where I need to run a background task that:
Continues running even if the user interacts with the app or navigates to another page.
Writes a file to disk upon completion (optional).
Sends a notification to the UI when it finishes.
I know that Streamlit runs in a single-threaded environment, but I was wondering if anyone has successfully implemented a background task that doesn’t stop when the user interacts with the UI. My idea was to use threading, asyncio, or even multiprocessing, but I’m not sure which approach would be best.
One possible workaround is to use a subprocess that keeps running independently of Streamlit and writes a file when it’s done. Then, the frontend could periodically check for that file. But ideally, I’d like a more elegant solution that allows the backend to notify Streamlit directly when the task completes.
Has anyone found a good way to do this? Possible Solution Using asyncio and threading
Since Streamlit runs a separate process for each user session, we need to ensure that our background task continues running independently of the session lifecycle. One way to achieve this is by using asyncio with threading to run the task in the background and then notify the frontend when it finishes.
Has anyone tried something similar?
I know there is another discussion about this topic:
I’ve used the file + subprocess solution you’re mentionning, on my end on carburoam, an open source app which exposes daily price of gas stations in France
Have a look at function trigger_etl from homepage triggered by visitors here and function main_etlhere
I had to implement a mecanism to refresh the local database of prices.
This was pretty straightforward to do wit this solution, and even if it doesn’t seems best practice to check file and do it this way, honestly it does works very well for a hobby/fun. project
If you’re looking to deep dive on the subject, I’ve written a blog post explaining details about this implementation and the workflow (ensure another process hasn’t started, monitoring, and so on..)
NB:Make sure to use python executable during subprocess calls, you will avoid some debugging issue or python env related issue when comparing local env during developement and Streamlit cloud one
If I had to pick some production grade orchestrator I would have used python rq along a redis pubsub service.
I’ve already battle tested it on production project but here with streamlit Cloud you couldn’t connect any external services easily. it’s only an option if you are using Render for instance
Streamlits built-in fragments and caching features might be a possible solution, but Streamlit does provide guidance on multithreading in the documentation which is listed below. I’ve found Fragments to be highly useful and would recommend atleast reviewing the execution flow page just for future reference.
Fragments, Caching, and Session State in Streamlit
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.