我正在使用新的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'])
答案 0 :(得分:0)
PyMongo 2.1中的ReplicaSetConnection不支持end_request();它将在2.2版本中在未来几周内发布。同时,在断开连接之前无需调用end_request()。断开将关闭所有插座。