Pymango失败了,但不会例外

2024-09-25 08:38:46 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是Pymongo的查询

import mong #just my library for initializing
collection_1 = mong.init(collect="col_1")
collection_2 = mong.init(collect="col_2")

for name in collection_2.find({"field1":{"$exists":0}}):
    try:
            to_query = name['something']
            actual_id = collection_1.find_one({"something":to_query})['_id']
            crap_id = name['_id']
            collection_2.update({"_id":id},{"$set":{"new_name":actual_id}},upset=True)
    except:
            open('couldn_find_id.txt','a').write(name)

所有这些都是从一个集合中获取一个字段,找到该字段的id并更新另一个集合的id。它可以工作大约1000-5000次迭代,但是周期性地失败,然后我必须重新启动脚本。你知道吗

 > Traceback (most recent call last):
 File "my_query.py", line 6, in <module>
 for name in collection_2.find({"field1":{"$exists":0}}):
 File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 814, in next
   if len(self.__data) or self._refresh():
 File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 776, in _refresh
   limit, self.__id))
 File "/home/user/python_mods/pymongo/pymongo/cursor.py", line 720, in __send_message
self.__uuid_subtype)
 File "/home/user/python_mods/pymongo/pymongo/helpers.py", line 98, in _unpack_response
cursor_id)
 pymongo.errors.OperationFailure: cursor id '7578200897189065658' not valid at server
 ^C
 bye

有人知道这个失败是什么吗?我怎么能把它变成一个例外,即使在这个失败的情况下继续我的脚本?你知道吗

谢谢


Tags: nameinpyselfidmodshomeline
1条回答
网友
1楼 · 发布于 2024-09-25 08:38:46

问题的原因在pymongo's FAQ中描述:

Cursors in MongoDB can timeout on the server if they’ve been open for a long time without any operations being performed on them. This can lead to an OperationFailure exception being raised when attempting to iterate the cursor.

这是因为collection.find()timeout参数:

timeout (optional): if True (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to False, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with timeout turned off are properly closed.

timeout=False传递给find应该可以解决问题:

for name in collection_2.find({"field1":{"$exists":0}}, timeout=False):

但是,请确保正确关闭光标。你知道吗

另请参见:

相关问题 更多 >