如何避免在内存中池化数据。何时在 pymongo 中迭代游标对象?

发布于 2024-10-09 02:20:35 字数 336 浏览 9 评论 0原文

如何避免在内存中池化数据。何时在 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 技术交流群。

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

发布评论

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

评论(2

只有影子陪我不离不弃 2024-10-16 02:20:35

你知道这是否可能吗?如果 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.

夏末染殇 2024-10-16 02:20:35

查看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.

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