How avoid pooling data in memory. When iterate cursor object in pymongo?

半世苍凉 提交于 2019-12-13 05:45:26

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4502246/how-avoid-pooling-data-in-memory-when-iterate-cursor-object-in-pymongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!