原因:在cursor上长时间没有进行操作,会引发cursor在mongodb服务端超时
解决办法:设置_no_cursor_timeout = True_,永不超时,游标连接不会主动关闭,需要手动关闭

pymongo

https://api.mongodb.com/python/current/migrate-to-pymongo3.html?highlight=timeout#timeout-replaced-by-no-cursor-timeout

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

mongoengine

http://docs.mongoengine.org/apireference.html?highlight=timeout#mongoengine.queryset.QuerySet.timeout

  1. for each in article.objects(**context).timeout(False):
  2. do_something()