Pas*_*que 12 google-app-engine app-engine-ndb
我正在将我的ndb代码库尽可能多地移动到异步中.有一种情况我不太清楚如何继续:交易.
在我看来,我有3种选择:
ndb.transaction()同步调用,并使事务的函数调用异步方法.def option_1():
@ndb.tasklet
def txn():
e = yield Foo.get_by_id_async(some_id)
if e is None:
e = yield Foo().put_async()
raise ndb.Return(e)
# The doc for ndb.transaction() says that it takes a function or tasklet.
# If it's a tasklet, does it wait on the Future that is returned?
# If it doesn't wait, what is the proper way to call a tasklet in a transaction
# and get access to its return value?
return ndb.transaction(txn)
Run Code Online (Sandbox Code Playgroud)
ndb.transaction()异步调用,并具有事务的函数调用同步方法.@ndb.toplevel
def option_2():
def txn():
e = Foo.get_by_id(some_id)
if e is None:
e = Foo().put()
return e
result = yield ndb.transaction_async(txn)
return result
Run Code Online (Sandbox Code Playgroud)
ndb.transaction()异步调用,并使事务的函数调用异步方法.@ndb.toplevel
def option_3():
@ndb.tasklet
def txn():
e = yield Foo.get_by_id_async(some_id)
if e is None:
e = yield Foo().put_async()
raise ndb.Return(e)
result = yield ndb.transaction_async(txn)
return result
Run Code Online (Sandbox Code Playgroud)
我觉得选项3是要去的,但我宁愿依赖专家意见/建议......
是的,#3绝对是要走的路.另外两个有问题,他们混合了异步和同步代码,除了在应用程序的最顶层,这通常不是一件好事.
PS.实际上,如果事务是一个tasklet,那么事务会等待回调.
| 归档时间: |
|
| 查看次数: |
1302 次 |
| 最近记录: |