Problem description:
When you use pymongo to connect to the MongoDB database for query operations, when the queried dataset is large, you need to maintain a long connection. If you do not set it, it will cause the crusor time out.
Solution:
Import pymongo, datetimedef readinstance (): Print 'read instances... 'Global conn_mongo DB = conn_mongo ['mongo _ db'] col_query = DB ['query'] Try: # In the find function, setting for item in col_meta.find (query, timeout = false ). sort ("_ id", pymongo. ascending): Print item ['_ id'] # facilitates the business process. This process takes a long time because of the large dataset. T: errinfo = sys. exc_info () print errinfo finally: # The connection must be released after use, otherwise it will always occupy system resources conn_0000.close () if _ name _ = '_ main _': start_ms = datetime. datetime. now () defaults _host = '2017. 168.4.81 'prop _port = 27017 conn_mongo = pymongo. connection (cmd_host, cmd_port) readinstance () end_ms = datetime. datetime. now () print 'cost: '+ STR (end_ms-start_ms ). seconds) + 's' pass
Specify timeout = false when executing the find function, and remember to release the connection after use.
Good luck