如何避免在内存中池化数据。何时在 pymongo 中迭代游标对象?
如何避免在内存中池化数据。何时在 pymongo 中迭代游标对象?
示例:
def iter():
c=pymongo.Connection()
cursor=c.db.media.find().skip(0).limit(50000)
for item in cursor:
yield item
在进入循环 for
之前,有大约 2 负的暂停。由于某种原因,它在开始迭代之前将所有数据加载到内存中。我能以某种方式避免它吗?
如果我在 mongodb shell 中执行此操作,则一切正常。
How avoid pooling data in memory. When iterate cursor object in pymongo?
Example:
def iter():
c=pymongo.Connection()
cursor=c.db.media.find().skip(0).limit(50000)
for item in cursor:
yield item
Before it goes in cycle for
there is pause about 2 minus. It loads all data in memory before start iterate for some reason. Can i somehow avoid it?
If I do it in mongodb shell everything is ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你知道这是否可能吗?如果 c.db.media.find() 返回所有内容而不是迭代器,我不确定您能做多少事情。
Do you know if this is possible? If c.db.media.find() returns everything instead of an iterator, I'm not sure there's much you can do.
查看cursor的block_size方法。有了它,您应该能够设置提前阅读的数量。我说应该,因为我现在面临一些问题(在pymongo中修改batch_size时在next(光标)上出现StopIteration异常),但我可能犯了一些错误。 block_size 应该可以解决你的问题。
Look at cursor's block_size method. With it you should be able to set how much you read in advance. I say should, because I'm facing some problems with it now (Getting StopIteration exception on next(cursor) when modifying batch_size in pymongo), but I'm probably making some mistake. block_size should solve your problem.