python报错:pymongo.errors.CursorNotFound: Cursor not found
程序员文章站
2024-02-10 13:56:22
...
python报错:pymongo.errors.CursorNotFound: Cursor not found
背景:
从数据库中取得所有数据 db['test'].find({},{_id:0}),然后对结果进行for循环
demos = db['demo'].find({},{"_id": 0})
for cursor in demos:
do_something()
但是当do_something函数耗时过长,在cursor上长时间没有进行操作,引发cursor在mongodb服务端超时,报错:pymongo.errors.CursorNotFound: Cursor not found
解决方案:
1、设置no_cursor_timeout = True,永不超时,游标连接不会主动关闭,需要手动关闭
demos = db['demo'].find({},{"_id": 0},no_cursor_timeout = True)
for cursor in demos:
do_something()
demo.close() # 关闭游标
2、设置batch_size返回文档数,默认应该是20个文档(记不清了233333),可以设置小一些
#每次只返回一个文档
demos = db['demo'].find({},{"_id": 0}).batch_size(1)
for cursor in demos:
do_something()
注意:这种方法仍然会出现可能超过10分钟任然没有返回,比如你在do_something里进行一些十分耗时的操作,具体采用哪种方法按实际情况而定
上一篇: python报错:pymongo.errors.CursorNotFound: Cursor not found
下一篇: pymongo.errors.CursorNotFound: Cursor not found, cursor id: 124891571478
推荐阅读
-
python报错:pymongo.errors.CursorNotFound: Cursor not found
-
pymongo.errors.CursorNotFound: cursor id 1598856597812 not found原因以及解决方式
-
python报错:pymongo.errors.CursorNotFound: Cursor not found
-
pymongo.errors.CursorNotFound: Cursor not found, cursor id: 124891571478
-
pymongo.errors.CursorNotFound: Cursor not found 错误处理
-
pymongo.errors.CursorNotFound: Cursor not found
-
Ubuntu中Pycharm配置anaconda报错:python packaging tools not found
-
Python报错-bash: pip: command not found
-
python调用webservice接口type not found报错
-
python 安装 scipy 报错 no lapack/blas resources found 问题处理