@ndb.tasklet
def get_data_parallel(e):
usr, det = yield (e.user.get_async(),
MyKind.query(ancestor = e.key).fetch_async())
raise ndb.Return((e, usr, det))
@ndb.taskletdef get_data_parallel(e):usr, det = yield (e.user.get_async() if e.user else None,MyKind.query(ancestor = e.key).fetch_async())raise ndb.Return((e, usr, det))
@ndb.tasklet
def get_cart_plus_offers(acct):
cart, offers = yield get_cart_async(acct), get_offers_async(acct)
raise ndb.Return((cart, offers))That
yield x, y
is important but easy to overlook. If that were two separateyield
statements, they would happen in series. Butyield
ing a tuple of tasklets is a parallel yield: the tasklets can run in parallel and theyield
waits for all of them to finish and returns the results. (In some programming languages, this is known as a barrier.)
Code#2:@ndb.taskletdef get_data(e):usr = yield e.user.get_async()det = yield MyKind.query(ancestor = e.key).fetch_async()raise ndb.Return((e, usr, det))
@ndb.taskletdef get_data_parallel(e):usr, det = yield (e.user.get_async(), MyKind.query(ancestor = e.key).fetch_async())raise ndb.Return((e, usr, det))