我正在使用新的 ReplicaSetConnection 方法连接到我的 mongodb 集群。更改实际上归结为用 pymongo.ReplicaSetConnection 替换 pymongo.Connection。我将连接用于我的目的,然后在连接上调用 end_request 以确保在连接上调用 disconnect() 之前刷新连接。这确保了长时间运行后我没有大量的半连接套接字。这在我使用 Connection 时效果很好,但是当我使用 ReplicaSetConnection 时,pymongo 抱怨说我正在尝试在数据库对象上运行 end_request(),尽管事实上我绝对是针对 ReplicaSetConnection 对象调用它。这是 pymongo 中的新功能还是驱动程序中的错误?以下是我遇到的问题的手动运行。
>>> import pymongo
>>> s = pymongo.ReplicaSetConnection("192.168.1.1:27017, 192.168.1.2:27017", replicaSet='rep1', safe=True)
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])
>>> s.read_preference = pymongo.ReadPreference.SECONDARY
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])
>>> type(s)
<class 'pymongo.replica_set_connection.ReplicaSetConnection'>
>>> d = s['test']
>>> s.end_request()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/pymongo/database.py", line 696, in __call__
TypeError: 'Database' object is not callable. If you meant to call the 'end_request' method on a 'Connection' object it is failing because no such method exists.
>>> s.disconnect()
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])