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!

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.