Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ This page only contains information on the `st.cache_resource` API. For a deeper

</Tip>

<Note>

Due to the current implementation of Streamlit’s caching mechanism, objects passed to
`st.cache_resource` must be **pickle-able** (serializable) in addition to being
**hashable**.

Streamlit’s rerun model relies on **content-based hashing** to determine when cached
values can be reused across reruns. This hashing mechanism is implemented using
Python’s pickling system (via the `__reduce__()` method). As a result, cached objects
must be pickle-able.

Objects that contain non-pickle-able elements (such as functions, lambdas, or open
file handles) may raise errors such as:

`TypeError: cannot pickle 'function' object`

</Note>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems OK to me, it might be good to go into a bit more detail on the reason for the implementation. For example,

Streamlit's rerun model requires content-based hashing which is provided by python's __reduce__() method which means that objects must not only be hashable but also pickle-able. Objects that contain non-pickleable elements (such as functions) may raise errors like TypeError: cannot pickle 'function' object.

I am wondering whether we should change the actual error. Do you have any suggestions @sfc-gh-dmatthews

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might go with a little less detail in this case. I updated the docstrings for both st.cache_data and st.cache_resource:

A function's arguments must be hashable to cache it. Streamlit makes a best effort to hash a variety of objects, but the fallback hashing method requires that the argument be pickleable, also. If you have an unhashable argument (like a database connection) or an argument you want to exclude from caching, use an underscore prefix in the argument name. In this case, Streamlit will return a cached value when all other arguments match a previous function call. Alternatively, you can declare custom hashing functions with hash_funcs.

@sfc-gh-lwilby From your discussions, it sounds like it's only the fallback method that requires the object to be pickable, so I went with that as the point to highlight. "We hash most things just fine, but if that fails, the object must be pickleable." It didn't sound like pickable was actually a requirement...until everything else failed. If I've understood correctly, I would specifically catch that error when __reduce__ is used to say that the object is being hashed by a fallback method that requires the item be pickleable. I see that hashing.py has a lot of TODO items around creating more, specific warnings, so I can chat with you to figure something out.


<Autofunction function="streamlit.cache_resource" oldName="streamlit.experimental_singleton" />

<Autofunction function="streamlit.cache_resource.clear" oldName="streamlit.experimental_singleton.clear" />
Expand Down