Python 为Datastore Plus(NDB)中的事务提供参数
在使用Datastore Plus时,我很难确定如何将参数传递到事务中 有人可以重写这个常规数据存储示例代码吗Python 为Datastore Plus(NDB)中的事务提供参数,python,google-app-engine,transactions,google-cloud-datastore,Python,Google App Engine,Transactions,Google Cloud Datastore,在使用Datastore Plus时,我很难确定如何将参数传递到事务中 有人可以重写这个常规数据存储示例代码吗 from google.appengine.ext import db class Accumulator(db.Model): counter = db.IntegerProperty() def increment_counter(key, amount): obj = db.get(key) obj.counter += amount obj.
from google.appengine.ext import db
class Accumulator(db.Model):
counter = db.IntegerProperty()
def increment_counter(key, amount):
obj = db.get(key)
obj.counter += amount
obj.put()
q = db.GqlQuery("SELECT * FROM Accumulator")
acc = q.get()
db.run_in_transaction(increment_counter, acc.key(), 5)
我特别感兴趣的是数据存储加上最后一行的等价物
的示例代码根本不处理参数(在事务中硬编码)。假设您可以从文档中学习示例,答案是使用lambda(或命名的helper函数)。例如
假设您可以遵循文档中的示例,答案是使用lambda(或命名的helper函数)。例如
yield context.transaction(lambda: increment_counter(acc.key(), 5))