[英]Google AppEngine - Big datastore reads
我需要阅读Google AppEngine数据存储区中的所有条目才能进行一些初始化工作。 有很多实体(目前为80k)并且这种情况继续增长。 我开始达到30秒的数据存储区查询超时限制。
是否有关于如何在数据存储区中对这些类型的大型读取进行分片的最佳实践? 任何例子?
您可以通过以下几种方式解决这个问题:
在Task Queue上执行你的代码,它有10分钟超时而不是30秒(实际上更像是60秒)。 最简单的方法是通过DeferredTask
。
警告 :DeferredTask必须是可序列化的,因此很难传递复杂的数据。 也不要让它成为一个内在的阶级。
见后端 。 后端实例提供的请求没有时间限制。
最后,如果你需要分解一个大任务并且并行执行而不是看mapreduce 。
StackExchange上的这个答案很好地为我服务:
我不得不稍微修改它以适合我:
def loop_over_objects_in_batches(batch_size, object_class, callback):
num_els = object_class.count()
num_loops = num_els / batch_size
remainder = num_els - num_loops * batch_size
logging.info("Calling batched loop with batch_size: %d, num_els: %s, num_loops: %s, remainder: %s, object_class: %s, callback: %s," % (batch_size, num_els, num_loops, remainder, object_class, callback))
offset = 0
while offset < num_loops * batch_size:
logging.info("Processing batch (%d:%d)" % (offset, offset+batch_size))
query = object_class[offset:offset + batch_size]
for q in query:
callback(q)
offset = offset + batch_size
if remainder:
logging.info("Processing remainder batch (%d:%d)" % (offset, num_els))
query = object_class[offset:num_els]
for q in query:
callback(q)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.