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.
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With