Can't cache object

I have a streamlit app that allows users to enter parameters to build a python object. This object can’t be pickled. I’m assuming this is because parts of the object are python wrapped C++ code (I tried dill too). I also notice that as the user continues with other parts of the app that use that python object, that object gets refreshed/rebuilt I’m assuming because it isn’t being cached (I had the app print out the objects address to notice it changed as the user continued entering in other data). Unfortunately, the python class has a randomly generated object in it that is needed later in the app. Since it keeps being refreshed, the app doesn’t work properly. For context, the python object is an encryption key generator object, and I need it to not be rebuild every refresh of the app. The earlier comment about pickling was because I tried to pickle and reload it at other points in the app. I also tried to use @st.cache decorator on the function call, but that had an error. I saw in the documentation that if there is something randomly generated in the function, caching is useless since the object is changing. If I need to reproduce the error for reference, let me know. So, to get to the exact question, is there a more thorough explanation anywhere of all the ins/outs of how to cache objects, or any other advice on how to save such an object for later use? Thank you!!! You all have been really helpful!

Hey @cjbumgardner: I’m glad I had the chance to chat with you about this offline. :slight_smile:

The basic procedure when you see this is to set the allow_output_mutation parameter in the cache decorator as follows:

@st.cache(allow_output_mutation=True)
def cached_func(...):
   ...

Please also note that allow_output_mutation is the new name for ignore_hash as of v0.49.0.

Thank you again Adrien! I’ll send a screenshot when I’ve got my app up and running!

2 Likes