GCP Datastore Python - InvalidArgument: 400非事务性提交可能不包含影响同一实体



我有一个云运行应用程序,定期写入记录到云数据存储。每个记录每次都有相同的键,所以我在写入时更新记录。
问题是这个应用程序会收到很多请求,因此会自动伸缩。当所有这些自动伸缩的实例写入云数据存储时,有时它们会同时尝试写入,这就是我看到下面提到的异常的时候:

google.api_core.exceptions.InvalidArgument: 400 A non-transactional commit may not contain multiple mutations affecting the same entity.

下面是上传函数的框架代码。

def datastore_upload(records: list):
client = datastore.Client()
kind = "some_kind"
entities = []
for record in records:
name = record['name']
key = client.key(kind, name)
task = datastore.Entity(key=key)
task['x'] = record['x']
task['y'] = record['y']
entities.append(task)
client.put_multi(entities)

您可以通过跟踪正在更新的实体来轻松解决此问题,例如

def datastore_upload(records: list):
client = datastore.Client()
kind = "some_kind"
entities = {}
for record in records:
name = record['name']
key = client.key(kind, name)
task = datastore.Entity(key=key)
task['x'] = record['x']
task['y'] = record['y']
# Keep the last update for each task.
entities[name] = task
client.put_multi(list(entities.values()))

相关内容

  • 没有找到相关文章

最新更新