Python 使用normalize_标记的意外行为
我在下面的测试中出现了意想不到的行为。很可能我误解了什么,但目前我没有想法,希望您能提供帮助。考虑下面的测试:Python 使用normalize_标记的意外行为,python,dask,Python,Dask,我在下面的测试中出现了意想不到的行为。很可能我误解了什么,但目前我没有想法,希望您能提供帮助。考虑下面的测试: # test passing an object from dask import delayed, compute, get, set_options # for testing the caching from dask.base import normalize_token from dask.cache import Cache set_options(delayed_pur
# test passing an object
from dask import delayed, compute, get, set_options
# for testing the caching
from dask.base import normalize_token
from dask.cache import Cache
set_options(delayed_pure=True)
def test_object_hash():
cache_tmp = cache.Cache(1e9)
# test that object hashing is working
class Foo:
a = 1
b = 2
@normalize_token.register(Foo)
def tokenize_foo(self):
return normalize_token((self.a, self.b))
global_list = list()
def add(foo):
print("here")
global_list.append(1)
return foo.a + foo.b
# first, verify the hashes are the same
myobj = Foo()
first = delayed(add)(myobj)
myobj2 = Foo()
second = delayed(add)(myobj2)
assert first.key == second.key
# don't test with streams since it should be the same result
# this better highlights the problem
compute(first, get=get)
compute(second, get=get)
assert global_list == [1]
第一个assert语句通过,但第二个语句不通过。我认为dask缓存了结果,因此使用相同dask键的计算只计算一次。这个代码中缺少什么东西吗?
注意,这一直在dask.distributed
中工作,因此这可能是API中的误解
谢谢 我回答了自己的问题。我没有正确注册缓存。我不得不加上一句话: cache.register() 如果有人对此有其他意见,尽管我很高兴听到。谢谢