2017-10-11 76 views
1

以下測試出現意外行爲。我很可能誤解了一些東西,但目前我沒有想法,並希望得到輸入。考慮以下測試:normalize_token出現意外行爲

# 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的誤解。

謝謝!

回答

1

我回答了我自己的問題。我沒有正確註冊緩存。我不得不添加該行: cache.register()

如果有人對此有其他意見,但我很高興聽到。謝謝。