Creating an asynchronous method with Google App Engine's NDB

前端 未结 1 461
感情败类
感情败类 2021-01-16 11:44

I want to make sure I got down how to create tasklets and asyncrounous methods. What I have is a method that returns a list. I want it to be called from somewhere, and immed

相关标签:
1条回答
  • 2021-01-16 12:24

    It's pretty hard to verify for yourself that the methods are running concurrently -- you'd have to put copious logging in. Also in the dev appserver it'll be even harder as it doesn't really run RPCs in parallel.

    Your code looks okay, it uses yield in the right place.

    My only recommendation is to name your function get_updates_for_user_async() -- that matches the convention NDB itself uses and is a hint to the reader of your code that the function returns a Future and should be yielded to get the actual result.

    An alternative way to do this is to use the map_async() method on the Query object; it would let you write a callback that just contains the to_dict() call:

    @ndb.tasklet
    def get_updates_for_user_async(userKey, lastSyncDate):
      noteQuery = ndb.gql('...')
      note_list = yield noteQuery.map_async(lambda note: note.to_dict())
      raise ndb.Return(note_list)
    

    Advanced tip: you can simplify this even more by dropping the @ndb.tasklet decorator and just returning the Future returned by map_async():

    def get_updates_for_user_Async(userKey, lastSyncDate):
      noteQuery = ndb.gql('...')
      return noteQuery.map_async(lambda note: note.to_dict())
    

    This is a general slight optimization for async functions that contain only one yield and immediately return the value yielded. (If you don't immediately get this you're in good company, and it runs the risk to be broken by a future maintainer who doesn't either. :-)

    0 讨论(0)
提交回复
热议问题