st.cache always shows a warning for mutated inputs, even if ignore_hash is True
See original GitHub issueSteps to reproduce
Get some data files:
python -m spacy download en_core_web_sm
Then run this:
import streamlit as st
import spacy
@st.cache(ignore_hash=True)
def load_model(name):
return spacy.load(name)
@st.cache(ignore_hash=True)
def process_text(nlp, text):
return nlp(text) # This mutates the nlp object. So we raise a warning.
nlp = spacy.load("en_core_web_sm")
doc = process_text(nlp, "Hello world")
What you see:
Cached function mutated its input arguments
When decorating a function with @st.cache, the arguments should not be mutated inside the function body, as that breaks the caching mechanism. Please update the code of process_text to bypass the mutation.
See the Streamlit docs for more info.
Expected
It’s OK if we have a warning for this case, but there should be a way to turn it off too.
Debug info
- Streamlit version: 0.45.0
Possible solutions
- Add kwarg
allow_input_mutation=True
that suppresses this warning and doesn’t check inputs when pulling from the cache. - This should actually be a different feature request, but if we added kwargs
clone_inputs=True
andclone_output=True
, which allows mutation.
Addendum: should we rename or change the semantics of ignore_hash
? There are different hashes in st.cache
, and ignore_hash
only ignores one of them.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Caching issues - Streamlit Docs
How to fix the Cached Object Mutated warning. By default Streamlit expects its cached values to be treated as immutable -- that cached...
Read more >Customizing the behavior of cached fields - Apollo GraphQL
A merge function that specifies what happens when field's cached value is written; An array of key arguments that help the cache avoid...
Read more >Advanced Streamlit Caching - Towards Data Science
When we mark a function with Streamlit's cache decorator @st.cache , whenever the function is called streamlit checks the input parameters ...
Read more >Streamlit Tips, Tricks, and Hacks for Data Scientists - Medium
It has been more than a year since the SSENSE data science team has been using Streamlit actively. Before employing Streamlit, we were...
Read more >Redux Essentials, Part 8: RTK Query Advanced Patterns
As with adding posts, the first step is to define a new mutation ... The <SinglePostPage> is still using the cached Post entry...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sam-qordoba : I believe this is the issue you’re seeing.
@sam-qordoba That’s interesting – I just double-checked again and the only difference between the two files are the unicode declarations (that should only be necessary for Python 2, so I didn’t bother adding them to my initial example). So I guess there’s something else going on here.