Why each function in this Streamlit code executes 3 times?

Hello,

Please advice how I can get my code executed only once during data load?

@st.cache() and @st.cache(allow_output_mutation=True) decorations doesn’t help.

see example below:

import streamlit as st
import pandas as pd

fname = "test.xlsx"


@st.cache(allow_output_mutation=True)
def prep_stage(fname):
    print("start reading excel data to memory")
    data = pd.read_excel(fname) 
    print("finished excel data read")
    return data

data = prep_stage(fname)

Received output:

start reading excel data to memory
start reading excel data to memory
start reading excel data to memory
finished excel data read
finished excel data read
finished excel data read

Thanks!

2 Likes

I’ve tried running this with streamlit run script.py and it only seems to run once for me. (Even if I try accessing the app through multiple browsers to load it many times.)

Of course I’m not using the same Excel file as you.

How are you running the app?

What happens if you comment out the pd.read_excel line, and just return an empty array or something instead?

1 Like

I found out that it was caused by accessing the app through multiple browsers at the same time.
As long as I was able convince my colleagues to close their browsers during app restart everything went back to normal. I wonder if streamlit developers can provide some sort of QUIESCE parameter for initial application startup to prevent users from using the app during initial heavy lifting.

Great to hear you found the problem at least.

I guess ideally, st.cache would have a lock so that if another thread is already running that function (with the same inputs) then it will just wait for the other’s results rather than start the calculation again.

I am facing the same issue, but I am running the streamlit app in one browser only and also I am not using any decorators. Is there a default number of threads run by streamlit?

Hello @Aishwarya_Verma , welcome to the community :slight_smile:

To be sure we’re talking about the same thing, would you be able to add a small reproducible code snippet with the bug, along with the browser you are using?

Cheers,
Fanilo

Thanks for replying. For demo, following is the code I executed,

import streamlit as st

from transformers import pipeline

#load the model

def get_model():

    """

    """

    classifier = pipeline('sentiment-analysis')

    return classifier

def generate_result(text):

    """

    """

    classifier = get_model()

    output = classifier(text)

    return output

print("Executing The streamlit App for Sentiment Analysis using Huggingface")

st.write("Enter your text")

user_input = st.text_input("")

if (st.button("Generating Output")):

    st.text("Please wait for 18-20 seconds, to get the output")

    output = generate_result(text=user_input)

    st.text(output)

And following is the number of times, this statement is printed.

Along with that, I am using Microsost Edge, But I have checked it on Firefox as well. There is no difference.

2 Likes