Is there a way to have a process run in the background independently of the page I'm using?

Hi all. I was trying to create a multipage streamlit dashboard for real time data analysis. I’ve created a thread that manages to call the API, but every time I change page I have to restart that thread. Is there a way to have it run independently of the page? Alternatively, is there a way to force streamlit to run some code each time a page is run?

Hi @Nicola_Lombardi

Have you tried looking into using the subprocess module? The popen method could be used to run commands.

Please refer to this example that runs R code in a Streamlit app:

Hope this helps!

Thank you so much Chanin! This is surely helpful, and I’ll integrate it in my app. I also have another issue, which is: what if my subprocess must load objects?
The app should work something like:

  • User interact with streamlit, at the same time
  • in the background, streamlit create subprocesses to run the API
  • in the background, streamlit loads the data obtained via the API

I’m currently looking if there’s a way to pass objects between streamlit and the subprocess. If you have any suggestions I’ll be grateful

Edit: I’ll also need a way for a new page to contact a subprocess, but I guess this can be done via session state

Hi @Nicola_Lombardi

Depending on the use case, I think objects if written as files, could be read/process by the app. Another approach could be to separate the subprocess and run via FastAPI that can then be accessed by the Streamlit app.

Please see this blog for some ideas: Streamlit🔥+ FastAPI⚡️- The ingredients you need for your next Data Science Recipe | by Afaque Umer | CodeX | Medium

And this repo: GitHub - davidefiocco/streamlit-fastapi-model-serving: Simple web app example serving a PyTorch model using streamlit and FastAPI

Hi, I ended up saving the objects as json, and passing the path to the object as a parameter. In this way I also worked around the maximum size of the strings in the PIPE between the main app and the subprocess.
Thank you so much. Nicola

2 Likes

Here’s a post that might be helpful Make apps faster by moving heavy computation to a separate process

1 Like

Glad to hear that it works now and thanks for sharing your solution.

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