一次将许多实体放入数据存储中

发布于 2024-12-26 07:27:18 字数 510 浏览 4 评论 0 原文

我有一个循环,将很多实体放入数据存储区,如下所示:

for line in open(file):
    if count >= limit:
        break
    elif count >= offset:
        prop1 = XXX
        prop2 = YYY
        MyEntity(prop1=XXX, prop2=YYY).put()
    count += 1

我知道如何使用 批量上传器,但我需要在代码中执行此操作.. 只要限制不是那么大,我目前所拥有的就有效(否则我会收到超过截止日期的错误,但这不是这个问题的范围),我只是问是否有更好或更有效的方法来做到这一点,因为这看起来像是一个丑陋的黑客,例如一种将所有实体放在一个镜头中而不是循环等的方法。

I have a loop where I put a lot of entities into the datastore, something like this:

for line in open(file):
    if count >= limit:
        break
    elif count >= offset:
        prop1 = XXX
        prop2 = YYY
        MyEntity(prop1=XXX, prop2=YYY).put()
    count += 1

I know about using the bulk uploader, but I need to do this from within the code..
What I currently have works as long as the limit is not that big (otherwise I get a deadline exceeded error, but that's not the scope of this question), I am only asking if there's a better or more efficient approach for doing this, as this seems like an ugly hack, for example an approach like putting all the entities in one shot instead of looping or so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2025-01-02 07:27:18

您正在为您存储的每个实体进行数据存储往返。相反,累积它们并执行单个批处理:

to_write = []
for line in open(file):
  #...
  to_write.append(MyEntity(prop1=XXX, prop2=YYY)
db.put(to_write)

如果您仍然需要将操作分成多个部分,请使用任务队列将每个工作块作为单独的任务排队。

You're doing a datastore round trip for each entity you store. Instead, accumulate them and do a single batch put:

to_write = []
for line in open(file):
  #...
  to_write.append(MyEntity(prop1=XXX, prop2=YYY)
db.put(to_write)

If you still need to break your operation up into multiple parts, use Task Queues to enqueue each chunk of work as a separate task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文