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
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?
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.
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?
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?
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.