OMG, Guido van Rossum answer my question.
I am waiting for official answer from Google/Alphabet
At our company, we use NDB a lot. The auto batching feature of NDB is so great. Thanks you a lot Guido van Rossum.
The library helps us make a code clean without sacrifice for performance.
But there are some cases we want to tune the auto batcher.
E.g: currently in NDB, query is run without batching, it makes all other actions after query is not batch
Let me make it more clear
In our app, we have model User, and Debt
class User(ndb.Model):
name = ndb.StringProperty()
class Debt(ndb.Model):
user_key = ndb.keyProperty(kind='User')
so now, if we run a code like this one
@ndb.tasklet
def get_debt_count(user_key):
key = '%s:debt_count' % user_key.urlsafe()
ctx = ndb.get_context()
value = yield ctx.memcache_get(key, use_cache=True)
if value is None:
value = yield Debt.query(Debt.user_key == user_key).count_async()
yield ctx.memcache_set(key, value, use_cache=True)
raise ndb.Return(value)
@ndb.tasklet
def get_user(user_key):
user, debt_count = yield user_key.get_async(), get_debt_count(user_key)
raise ndb.Return(user, debt_count)
@ndb.tasklet
def get_users_info(user_keys):
result = yield map(get_user, user_keys)
raise ndb.Return(result)
Now, if we want to get info of 10 users, the codeget_users_info(user_keys).get_result()
will run with
+ 1 memcache.get user, and debt_count cached from
+ 10 query to Debt to get debt count if debt count is not cached
+ 10 memcache.set to set debt count int cached
What we want is an option to tell context wait after queries, and batch all 10 memcache.set into 1 query.
To do that, I think I could make an auto batcher for these Debt queries, but the flush of that auto batcher is making all queries in batch run parallelly.
Is there any way to do that without change the internal of NDB library?