Running two functions simultaneously

Hello everyone,

I have a query. There is a function which have 3 steps,
Step A, Step B, Step C
The above function is initiated when we click a โ€œSubmitโ€ button. (Its more kind of a form which will analyze input data along with the progress bar)

I need to update the progress bar as steps gets completed in the backend (running on different port) of the code.

Currently, I am appending the status of the three steps on the .txt file and the streamlit app is reading it and updating the progress bar.

I want to do perform both the action simultaneously. Can you please help me how to proceed ?
Below is the sample code I am trying, however the progress bar is updated after the completion of the function, is it possible to run both the functions parallelly ?

import os
import time

import streamlit as st

dir = r"D:\\Workspace\backend"

progress_bar = st.progress(0)
st.spinner()
status_text = st.empty()

status_file = open(dir + "\\status_1.txt", "w")


def process_data():
    time.sleep(10)
    status_file.write("Step A Completed | 200\n")
    time.sleep(10)
    status_file.write("Step B Completed | 200\n")
    time.sleep(10)
    status_file.write("Step C Completed | 200\n")
    status_file.close()
    return None


process_data()

while True:
    time.sleep(5)
    with open(dir + os.path.sep + "status_1.txt", 'r') as f:
        status_data = f.readlines()
        print("2", status_data)
    if len(status_data) == 0:
        progress_bar.progress(0)
        status_text.text('Starting')
    elif len(status_data) == 1:
        progress_bar.progress(33)
        status_text.text('Completed Step A')
    elif len(status_data) == 2:
        progress_bar.progress(66)
        status_text.text('Completed Step B')
    elif len(status_data) == 3:
        progress_bar.progress(100)
        status_text.text('Completed Step C')
        break

Is there any better way to do it ?

Appreciate your help and time. Thank you.

2 Likes