Hi @luca,
I though that since the function does not have any parameters it would be called only once, the first time
Sorry I’m not sure I understand what you mean here. Are you referring to the get_db
function? It will be called on the first run of the report, after which we’ll return the plyvel.DB()
connection from the cache when it’s called.
Is it doing some internal check to see if the returned object is mutated, and in case return a new object?
Previous versions of Streamlit did this but as of version v0.53.0 we still do this internal check but we display a warning and return the cached version of the object instead of re-running the function and returning a new object.
https://github.com/streamlit/streamlit/blob/0.53.0/lib/streamlit/caching.py#L286
The same code works even with my wrapper, if I set
hash_funcs={LevelDB: id}
orallow_output_mutation=True
I believe we disable hashing of the output if you set allow_output_mutation
to True
, which in this case negates the need to use hash_funcs
to allow for the hashing of the plyvel.DB instance. However if you wanted to pass this db instance to another cached function as an input parameter, or to use the instance in the body of a cached function (not the return value), you would need to use hash_funcs
as allow_output_mutation
would not help in those scenarios. I would stick with hash_funcs
either way as allow_output_mutation
would be used as a hack rather than for its primary use case
https://github.com/streamlit/streamlit/blob/0.53.0/lib/streamlit/caching.py#L373