类中具有Streamlight的缓存函数



我有以下类,其函数"expensive_计算s我想缓存的结果:

class c:
def download_from_GCS(self, b, c):
print(b + c)

@st.cache(suppress_st_warning=True)
def expensive_computation(self, a):
self.download_from_GCS(b=a, c=a+10)
print(a)

c().expensive_computation(a=1)

在没有Streamlit缓存指示的情况下,代码正常工作;然而,一旦引入,就会出现以下错误:

InternalHashError: module '__main__' has no attribute '__file__'
While caching the body of `expensive_computation()`, Streamlit encountered an
object of type `builtins.function`, which it does not know how to hash.
In the meantime, you can try bypassing this error by registering a custom
hash function via the `hash_funcs` keyword in @st.cache(). For example:
```
@st.cache(hash_funcs={builtins.function: my_hash_func})
def my_func(...):
...
```
If you don't know where the object of type `builtins.function` is coming
from, try looking at the hash chain below for an object that you do recognize,
then pass that to `hash_funcs` instead:
```
Object of type builtins.function: <function c.expensive_computation at 0x7fe84b35d0d0>
```
Please see the `hash_funcs` [documentation]
(https://docs.streamlit.io/library/advanced-features/caching#the-hash_funcs-parameter)
for more details.

我试过替换@st.cache(hash_funcs={builtins.function:"function c.expensive_computation at 0x7fe84b35d0d0"}),但似乎不起作用。

我如何才能正确地缓存";expensive_ computation";使用Streamlight?

Streamlit的更新版本中修复。

输出:

12
1

最新更新