We want to deprecate st.cache! …and need your input ❤️

I’m new to Streamlit, but my biggest pain with st.cache and st.experimental_memo has been trying to resolve the errors that come when Streamlit isn’t able to hash or pickle something.

st.experimental_memo

@st.experimental_memo(ttl=300)
def initMysqlConnection():
    return mysql.connector.connect(**st.secrets["mysql"])

throws:

TypeError: cannot pickle '_mysql_connector.MySQL' object
Traceback:
File "/Users/gagandeep/work/wfhtoday-st/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
File "/Users/gagandeep/work/wfhtoday-st/Home.py", line 174, in <module>
    main()
File "/Users/gagandeep/work/wfhtoday-st/Home.py", line 126, in main
    _conn = initMysqlConnection()

st.cache

Sometimes it’s independent of the parameters being passed to the function, like this:

@st.cache
def initMysqlConnection():
    return mysql.connector.connect(**st.secrets["mysql"])
UnhashableTypeError: Cannot hash object of type _thread.RLock, found in the body of initMysqlConnection().

While caching the body of initMysqlConnection(), Streamlit encountered an object of type _thread.RLock, which it does not know how to hash.

st.experimental_singleton

st.experimental_singleton works for caching mysql connections, but I observed some connection errors in long running apps, and had to clear the cache and refresh. I’m generally wary of singletons in principle

It would be nice if you could come up with an annotation to cache an object as is. Ignoring the function arguments starting with _ should be good enough.

4 Likes