zoukankan      html  css  js  c++  java
  • Python报错:pymongo.errors.CursorNotFound: Cursor not found

    python报错:pymongo.errors.CursorNotFound: Cursor not found

    背景:

    从数据库中取得所有数据 db['test'].find({},{_id:0}),然后对结果进行for循环

    1.  
      demos = db['demo'].find({},{"_id": 0})
    2.  
      for cursor in demos:
    3.  
      do_something()

    但是当do_something函数耗时过长,在cursor上长时间没有进行操作,引发cursor在mongodb服务端超时,报错:pymongo.errors.CursorNotFound: Cursor not found

    解决方案:

    1、设置no_cursor_timeout = True,永不超时,游标连接不会主动关闭,需要手动关闭

    1.  
      demos = db['demo'].find({},{"_id": 0},no_cursor_timeout = True)
    2.  
      for cursor in demos:
    3.  
      do_something()
    4.  
      demo.close() # 关闭游标

    2、设置batch_size返回文档数,默认应该是20个文档(记不清了233333),可以设置小一些

    1.  
      #每次只返回一个文档
    2.  
      demos = db['demo'].find({},{"_id": 0}).batch_size(1)
    3.  
      for cursor in demos:
    4.  
      do_something()

    注意:这种方法仍然会出现可能超过10分钟任然没有返回,比如你在do_something里进行一些十分耗时的操作,具体采用哪种方法按实际情况而定

    via:https://www.jianshu.com/p/a8551bd17b5b

    转载自:https://blog.csdn.net/weixin_41287692/article/details/82804123

  • 相关阅读:
    模板语言的作用及实例
    模板语言
    轮播图实例
    render,render_to_redponse,locals,redirect重定向
    setting中三个重要参数
    python中的Celery基本使用
    python中的Redis基本使用
    DRF之JWT认证
    DRF之过滤排序分页异常处理
    DRF之权限和频率限制
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/12205964.html
Copyright © 2011-2022 走看看