I am using a backend to write multiple entities with ndb.put_multi(list_of_entities)
.
The issue that I am experiencing is that just after that if I make a query then I get no results. If I put a sleep timer for eg 1 sec, I can read the entities that I just wrote.
So eg:
class Picture(ndb.Expando):
pass
class Favourite(ndb.Expando):
user_id = ndb.StringProperty(required=True)
pass
#...make lists with Picture and Favourite kinds
entities = favourites
entities[1:1] = pictures
ndb.put_multi(entities)
favourites = Favourite.query().filter(Favourite.user_id == user_id).fetch(99999, keys_only=True)
logging.info(len(favourites)) #returns 0 in dev_appserver why?
First assumed the problem has to do with caching. But:
Reading NDB Entities Operations on Multiple Keys or Entities:
Advanced note: These methods interact correctly with the context and caching; they don't correspond directly to specific RPC calls.
Reading NDB Caching
The In-Context Cache
The in-context cache persists only for the duration of a single incoming HTTP request and is "visible" only to the code that handles that request. It's fast; this cache lives in memory. When an NDB function writes to the Datastore, it also writes to the in-context cache. When an NDB function reads an entity, it checks the in-context cache first. If the entity is found there, no Datastore interaction takes place.
Queries do not look up values in any cache. However, query results are written back to the in-context cache if the cache policy says so (but never to Memcache).
Hm I am lost here. Everything seems to be ok. Even if query from the console I get the correct sum, but never on the same handler, no matter what function etc.
Only thing I noticed is that when put a wait time.sleep(1)
then I get the correct results. So that has to do with the fact that the ndb.put_multi
might not complete synchronously or not. So confused....
A clear mind in the morning is always better than a dizzy mind at night.
Thank you all for the comments. Problem solved. You lead me in the right way so to answer my question:
I used ancestor queries to get the results correctly. It's worth to mention the following
Understanding NDB Writes: Commit, Invalidate Cache, and Apply
That means that after each put the apply phase might not have completed.
And:
Also some things about consistency between read and writes taken from Google Academy Retrieving data from the Datastore
Thanks to @Paul C for constantly helping and @dragonx and @sologoub for helping me understand.