Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDB doesn't return same instance for asynchronous gets when memcache is enabled

My program relies on the NDB context cache so that different ndb.Key.get() calls will receive the same model instance.

However, I discovered that this doesn't work properly with asynchronous gets. The expected behavior is that NDB's batcher combines the requests and return the same model instance but that doesn't happen.

The problem only occurs when memcache is enabled which is also strange.

Here is a test case (run it twice):

class Entity(ndb.Model):
    pass

# Disabling memcache fixes the issue
# Entity._use_memcache = False

entity_key = ndb.Key('Entity', 1)

# Set up entity in datastore and memcache on first run
if not entity_key.get():
    entity = Entity(key=entity_key)
    entity.put()

    return

# Clear cache after Key.get() above
ndb.get_context().clear_cache()

# Entity is now in memcache and datastore but not context

entity_future_a = entity_key.get_async()
entity_future_b = entity_key.get_async()

entity_a = entity_future_a.get_result()
entity_b = entity_future_b.get_result()

# FAILS
assert entity_a is entity_b

So far I have only tested this on the local SDK.

like image 603
Nikolaj Avatar asked Jul 28 '26 05:07

Nikolaj


1 Answers

It is possible that this is happening because you are not calling yield in there. Can you try setting up the environment so you can use

entity_a, entity_b = yield entity_future_a, entity_b_future

?

like image 97
Guido van Rossum Avatar answered Jul 30 '26 09:07

Guido van Rossum