python - 事务性 Tasklet 中的非事务性操作

标签 python google-app-engine app-engine-ndb

我正在使用 Google App Engine SDK 1.7.3 和 NDB 来访问数据存储区。

我应该如何从事务性tasklet 中运行非事务性tasklet?

我首先想到的是以下内容:

@ndb.tasklet
@ndb.non_transactional
def non_txn_method():
    ''' Run some code that does not require a transaction '''
    result = calculate_result()
    raise ndb.Return(result)

@ndb.tasklet
@ndb.transactional
def txn_method():
    ''' Run some code that requires a transaction '''
    non_txn_result = yield non_txn_method()

但是 NDB 的创建者 advises不要像 @ndb.tasklet 那样组合装饰器和@ndb.transactional并使用 ndb.transaction_async(callback, **ctx_options)函数代替。请参阅here对于这样做的问题。因此,我担心将 @ndb.tasklet 结合起来和@ndb.non_transactional装饰器也可能很脆弱并且容易出现意外行为。

请注意ndb.transaction_async(callback, **ctx_options) NDB API 中提供但不是ndb.non_transaction_async(callback, **ctx_options) .

我想知道是否有 ndb.non_transaction_async(callback, **ctx_options)需要在 NDB API 中公开函数来帮助我稳健地完成我想做的事情吗?

最佳答案

请注意,以下装饰器顺序按预期工作(至少自 1.7.3 起):

@ndb.non_transactional
@ndb.tasklet
def non_txn_method():
  # If calling old db, need this too:
  datastore._SetConnection(None)
  # ...

由于此 bug 需要旧的数据库解决方法.

关于python - 事务性 Tasklet 中的非事务性操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13878594/

相关文章:

python - GAE NDB "Result cannot be set twice"错误

python - 使用正规方程的线性回归

python - 替换部分字符串

python - HTTP 检索错误 Twilio

python - 顺序模型在每次运行时给出不同的结果

Python:Google App Engine 源代码使用标签深度 2

python - 谷歌应用引擎 'No module named pwd'

google-app-engine - 如何在 gae、ndb 中使用 memcache 缓存分页查询?

python - 在 App Engine Python 中设置读取策略

java - Google App Engine 中的配额控制