When can loading be cached?

I have two code snippets in different projects, which seem functionally identical, but caching works for one, but doesn’t work for the other, and I don’t understand why.

This code snippet loads in a pickled dict. Streamlit reruns this at every execution, seemingly ignoring the cache command:

@st.cache
def getData():
    return  pickle.load(open(pickleFile,"rb")).data.numpy()

p = getData()

In contrast, Streamlit correctly caches this operation, which loads in a PyTorch tensor:

@st.cache
def loadW(suppress_st_warning=True):
	st.write('loading')

	return pickle.load(open(pickleFile,"rb")).data.numpy()

W = loadW()

I’ve moved these into .py files which do nothing but the above, and this still happens. pickleFile is constant in both files. (Note that the st.write does not affect this, I’ve tried it both ways.)

In my reading of the docs, these should both cache, because none of the inputs are changing. (The files are obviously not changing as well.) Why does the first snippet rerun on each execution?

1 Like

Hi @hertzmann,

Could you share the two pickleFiles?

Why do you think the getData function is ignoring the cache command?

I’ve just revisited this to try to isolate the issue further… and now caching is operating correctly. No changes to any of the files. Weird.

1 Like

Had the same problem with pickle.load()
Adding (allow_output_mutation=True) to the @st.cache decorator fixed it for me :slight_smile: