Best (current) practice for spawing a process to run independent of Streamlit?

Solved, subprocess works perfectly but had to use:

if st.button("Process data"):
  
  subprocess.Popen(['python','processor.py',selected_file])

I know similar issues has been posted before, but I couldnā€™t relate it to what I am trying to achieve.

Letā€™s say a user has a st.button they can click. Upon clicking they tell the underlying server to start processing a large file that might take ~24 hours to process and they might get a brief st.success message ā€œProcessing has started, please come back laterā€.

*After clicking the button the user might still want to use the app for other tasks and will come back the next day to check the results of the first file. *

What is the current best practice to spawn in underlying process and keep using streamlit (with having the constant running)? This approach underneath just seems to leave the current streamlit window running:

sudo code:

```

  • import subprocess*
  • import os*

raw_files = os.listdir(ā€œraw_data/ā€)
selected_file = st.selectbox(ā€œSelect file to processā€,raw_files )
if st.button(ā€œProcess dataā€):

  • subprocess.run(f"python processor.py {selected_file}", shell=True)*
    ```

Any ideas?

1 Like

Hi,

You can use this approach here, which spawns a Flask server, and calls its /foo endpoint from Streamlit. You could replace that endpoint implementation with your long running process (LRP). Via an additional /status endpoint you can occasionally request the status of the LRP from Streamlit. Your LRP could occasionally drop a status message file as it does its work for /status to read or maybe your LRP can be queried directly.

For something more robust run and monitor your LRP, use a job queue system, and call their APIs from Streamlit to get job statuses. Examples (for Python) that come to mind are:

HTH,
Arvindra

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