#Streamlit: Running functions simultaneously with streamlit in Windows

1 messages · Page 1 of 1 (latest)

mint sigil
#

I'm having a couple of issues running some functions simultaneously, as it spams this warning:
WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
There's also this:
WARNING streamlit.runtime.state.session_state_proxy: Session state does not function when running a script without `streamlit run`

E.g. I have a Training mode in my app, where it trains a detection model based on given parameters:

def training_mode():
    st.title("YOLO Model Training")

    parameter_col, graph_col = st.columns([1, 3])

    with parameter_col:
        cfg = st.text_input("Model Configuration File (e.g., yolov8-M.yaml)", "cfg/yolov8-M.yaml", key="cfg")
        data = st.text_input("Dataset YAML File", "data/Vis-DiceX2.yaml", key="data")
        epochs = st.number_input("Number of Epochs", min_value=1, max_value=500, value=150, step=10, key="epochs")
        imgsz = st.number_input("Image Size", min_value=64, max_value=1024, value=640, step=64, key="imgsz")
        batch = st.number_input("Batch Size", min_value=1, max_value=64, value=2, step=1, key="batch")
        name = st.text_input("Training Session Name", "MyTrain", key="name")

        start_training = st.button("Start Training")

    if start_training:
        st.write("Training Parameters:")
        st.write(f"Configuration File: {cfg}")
        st.write(f"Dataset: {data}")
        st.write(f"Epochs: {epochs}")
        st.write(f"Image Size: {imgsz}")
        st.write(f"Batch Size: {batch}")
        st.write(f"Session Name: {name}")

        results_file = f"runs/detect/{name}/results.csv"

        train_model(cfg, data, epochs, imgsz, batch, name, verbose=False)

        training_thread = threading.Thread(target=train_model)
        training_thread.start()

        with graph_col:
            st.title("Training Progress")
            display_training_progress(results_file)
#

When running this code, streamlit spams this error

#

This particularly occurs during a certain phase when running the training phase using ultralytics

golden pumice
#

@mint sigil
The issue you're encountering with the Streamlit warnings is likely due to the use of threading to run a background task (train_model). Streamlit doesn't support threading well for background processes because it operates on a single-threaded architecture that manages its own execution context, which conflicts with custom threads.

#

To resolve this, you can avoid threading and instead use an external library like multiprocessing or handle the training phase with a non-blocking callback system supported by Streamlit itself.

Here’s a safer and cleaner implementation: