2014-04-29 107 views
0

我有一個名爲Conversation一些領域,包括date_createddate_updatedDateTimeProptertyauto_now_addauto_now模型。GAE蟒蛇NDB put_async問題

如果我更新使用put()方法模型,date_updated場得到更新。 但是,當我使用put_async方法時,date_updated字段中的值未更新。

而且我也有使用Python的unittest.Testcase的測試用例,它工作正常。

注意:它適用於我使用put_async().get_result()

樣品模型類:

class Conversation(ndb.Model): 

    participants = ndb.StringProperty(repeated=True) 
    conversation_name = ndb.StringProperty() 
    date_created = ndb.DateTimeProperty(required=True, auto_now_add=True) 
    date_updated = ndb.DateTimeProperty(required=True, auto_now=True) 

    @staticmethod 
    def update_conversation_date_by_id(conversation_id): 
     conversation = Conversation.get_by_id(conversation_id) if conversation_id else None 
     if conversation is None: 
      raise CannotFindEntity("Given conversation_id is not found") 
     else: 
      conversation.put_async().get 
     return conversation 
+0

可能重複http://stackoverflow.com上/問題/ 11621313/IS-NDB-異步保證對執行-後的應用程序請求-具有成品) –

回答

4

如果請求處理程序退出前的NDB放完成認沽可能永遠不會發生。

class MyRequestHandler(webapp2.RequestHandler): 
    def get(self): 
    acct = Account.get_by_id(users.get_current_user().user_id()) 
    acct.view_counter += 1 
    future = acct.put_async() 
    # ...read something else from Datastore... 
    template.render(...) 
    future.get_result() 

嘗試添加類似該代碼塊中最後一行的內容以強制其等待。

在這個例子中,調用future.get_result有點愚蠢: 應用程序從不使用NDB的結果。該代碼僅在 以確保在NDB將 完成之前請求處理程序不會退出;如果請求處理器過早退出,則可能永遠不會發生 。作爲一種方便,您可以用 @ ndb.toplevel來裝飾請求處理程序。這告訴處理程序不要退出,直到其異步請求完成 。這又可以讓你發送 的請求,而不用擔心結果。

https://developers.google.com/appengine/docs/python/ndb/async

0

異步方法停止主線程停止時執行(請求處理程序結束)...所以很可能是你的代碼犯規執行。 這可以通過將@ ndb.toplevel添加到您的請求處理程序來防止。那麼處理程序將等待您的異步完成。

https://developers.google.com/appengine/docs/python/ndb/async

異步犯規讓你運行的代碼完成請求處理程序時。異步,您可以運行其他(異步)命令在等待異步命令相同的請求處理線程:)

的[NDB是異步保證應用程序的請求完成後執行?](