我加入了一個頻繁更改的專案,專案中已經完成了開發,它看起來像一個單體架構!有時我們在子模型中使用祖先模型中的某些欄位來處理某種過濾和排序功能(我之前問過它,除了使用重復欄位之外沒有向我介紹任何解決方案。鏈接)因此我們需要更新這些父級更新時的祖先欄位,由于專案很大,我決定使用模型信號而不是查找所有位置并更新源代碼以更新新模型中的這些欄位,我使用模型類_post_put_hook的和_pre_put_hook信號來完成此功能.google ndb
class ParentOne(ndb.Model):
name_parent_one = ndb.StringProperty()
....
def _post_put_hook(self, future):
obj = JoinParentNameClasses.query(JoinParentNameClasses.parent_one == self.key).get()
obj.name_parent_one = self.name_parent_one
obj.put()
class ParentTwo(ndb.Model):
name_parent_two = ndb.StringProperty()
....
def _post_put_hook(self, future):
obj = JoinParentNameClasses.query(JoinParentNameClasses.parent_two == self.key).get()
obj.name_parent_two = self.name_parent_two
obj.put()
class JoinParentNameClasses(ndb.Model):
parent_one = ndb.KeyProperty(kind='ParentOne')
name_parent_one = ndb.StringProperty()
parent_two = ndb.KeyProperty(kind='ParentTwo')
name_parent_two = ndb.StringProperty()
... some other fields which was used for API ....
但是現在出現了更大的問題:當有人嘗試使用put_multiorput_multi_async時google ndb,NDB會創建大量future物件并將它們發送給處理,ndb.tasklet然后.get_result()獲取最新的更新結果,根據ndb.tasklet實作,似乎python coroutines當我們有很多行時需要更新然后關于_post_put_hook更新的孩子,創建很多深度并maximum recursion depth exceeded出現。我該如何解決這個問題?
筆記。我知道sys.setrecursionlimit(1000)但這不是一個好的解決方案,我正在尋找最佳實踐。
uj5u.com熱心網友回復:
使用_post_put_hook似乎是在自找麻煩......串行放置是一個壞主意,因為它很慢并且會增加資料存盤爭用。您可能還創建了一個無限回圈。
相反,您希望將您的 put 批處理:
class ParentOne(ndb.Model):
def put_me(self):
obj = JoinParentNameClasses.query(JoinParentNameClasses.parent_one == self.key).get()
obj.name_parent_one = self.name_parent_one
ndb.put_multi([self, obj])
您提到您已經在使用ndb.put_multi觸發所有_post_put_hook呼叫的方法。相反,撰寫一個自定義函式
- 接收物件串列
- 獲取所有相關的父物件
- 將它們全部放在一個
ndb.put_multi
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491249.html
標籤:Python 数据库 谷歌应用引擎 应用引擎-ndb 谷歌应用引擎 python
