How to Run a Background Task in Streamlit and Notify the UI When It Finishes

Hi everyone,

I’m working on a Streamlit app where I need to run a background task that:

  1. Continues running even if the user interacts with the app or navigates to another page.
  2. Writes a file to disk upon completion (optional).
  3. 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:

Looking forward to your thoughts!

Thanks!

2 Likes

Hi Victor :waving_hand:

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 :fuelpump:

Have a look at function trigger_etl from homepage triggered by visitors here and function main_etl here :technologist:

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 :raised_hands:

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..) :closed_book:

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 :light_bulb:

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 :+1:

4 Likes

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

Multithreading in Streamlit

1 Like