mongo游标超时问题

import time

import pymongo

client_6 = pymongo.MongoClient("mongodb://localhost:27017", maxPoolSize=2000, waitQueueTimeoutMS=100)
session_6 = client_6.start_session()
session_id_6 = session_6.session_id
refreshTimestamp = time.time()


def refresh():
    """
    每5分钟刷新连接,避免游标超时或者连接失效,需要在耗时长的循环中调用
    参考 https://docs.mongodb.com/manual/reference/method/cursor.noCursorTimeout/
    """
    global refreshTimestamp
    if (time.time() - refreshTimestamp) > 60 * 5:
        print("refreshing session")
        client_6.admin.command({"refreshSessions": [session_id_6]})
        # res = client_6.admin.command("refreshSessions", [session_id_6])  # 或者使用这种形式
        refreshTimestamp = time.time()


cursor = session_6.client["gpf"]["0928"].find()
for i in cursor:
    print(i)
    refresh()
    time.sleep(10)


你可能感兴趣的:(mongo游标超时问题)