def push_queue_worker_async(): """ push_queue_worker_async() VS push_queue_worker_async2() (1) if a function uses "yield" it should ndb.tasklet, ndb.synctasklet, or ndb.toplevel. (2) a function wrapped in ndb.tasklet returns a Future (and you can either yield it or call get_result() explicitly). (3) ndb.synctasklet is like wrapping it in ndb.tasklet but implicitly calling get_result(). (4) ndb.toplevel is just like ndb.synctasklet but also waits for all pending operations to complete. ndb.tasklet function returns a future object which can be "yield" or get_result() As shown in push_queue_worker_async2(), if ndb.toplevel is not used, just explicitly call get_result() to get the result from a future object. If the result is not available, get_result() will block the thread until the result is ready. """ print "push_queue_worker_async() @ the backend module is called! " + request.values['email'] ctx = ndb.get_context() ctx.set_memcache_policy(False) # disable memcache ctx.set_cache_policy(lambda key: key.kind() == 'UserAccount') # enable in-context cache only for UserAccount email = request.values['email'] user_account = yield data_store.query_user_async(email) if not user_account: user_account = UserAccount(email=email, login_count=1) else: user_account.login_count += 1 user_account_key = user_account.put() raise ndb.Return("ok")
def index(): print "async index page!" if 'email' in flask.session: user_account = yield data_store.query_user_async(flask.session['email']) most_login_email, least_login_email = yield data_store.query_most_and_least_login_emails_async() if user_account: raise ndb.Return(render_template('hello.html', login_count=user_account.login_count, most_login_email=most_login_email, least_login_email=least_login_email)) raise ndb.Return(render_template('hello.html'))
def push_queue_worker_async2(): print "push_queue_worker_async2() @ the backend module is called! " + request.values['email'] email = request.values['email'] user_account = data_store.query_user_async(email).get_result() if not user_account: user_account = UserAccount(email=email, login_count=1) else: user_account.login_count += 1 user_account_key = user_account.put() return "ok"