PyMongo发送"认证"对于每个查询

时间:2015-02-13 19:34:09

标签: mongodb pymongo mongoengine python-rq

我正在使用PyMongo MongoClient运行的每个查询都对MongoDb进行身份验证。这似乎很昂贵/不必要:

2015-02-13T09:38:08.091-0800 [conn375243]  authenticate db: { authenticate: 1, user: "", nonce: "xxx", key: "xxx" }
2015-02-13T09:38:08.876-0800 [conn375243] end connection xxx (15 connections now open)
2015-02-13T09:38:08.962-0800 [initandlisten] connection accepted from xxx:42554 #375244 (16 connections now open)
2015-02-13T09:38:08.966-0800 [conn375244]  authenticate db: { authenticate: 1, user: "", nonce: "xxx", key: "xxx" }

据我所知,我使用的是相同的MongoClient(尽管它隐藏在MongoEngine背后)并且不会故意在任何时候断开它:

19:20:45 {'default': MongoClient('xxx-a0.mongolab.com', 39931)}
19:20:45 [139726027002480]
19:28:35 {'default': MongoClient('xxx-a0.mongolab.com', 39931)} # print mongo_client_instance
19:28:35 [139726027002480] # print id(mongo_Client_instance)

当我在authenticate函数中设置pdb断点时,这就是堆栈跟踪。我无法弄清楚为什么要求光标刷新需要新的身份验证。我误解了,这是MongoDb协议的一部分吗?我的目标是尽可能少地验证"尽可能发送命令,因为现在他们在服务器上记录了50%的已记录命令。

 /home/ubuntu/workspace//metadata/jobs.py(24)get()
-> b = Item.objects.get_or_create(id=i['id'])[0]
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/mongoengine/queryset/base.py(241)get_or_create()
-> doc = self.get(*q_objs, **query)
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/mongoengine/queryset/base.py(182)get()
-> result = queryset.next()
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/mongoengine/queryset/base.py(1137)next()
-> raw_doc = self._cursor.next()
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/cursor.py(1058)next()
-> if len(self.__data) or self._refresh():
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/cursor.py(1002)_refresh()
-> self.__uuid_subtype))
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/cursor.py(915)__send_message()
-> res = client._send_message_with_response(message, **kwargs)
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py(1194)_send_message_with_response()
-> sock_info = self.__socket(member)
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py(922)__socket()
-> self.__check_auth(sock_info)
  /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/mongo_client.py(503)__check_auth()
-> sock_info, self.__simple_command)
> /home/ubuntu/workspace//venv/local/lib/python2.7/site-packages/pymongo/auth.py(239)authenticate()
-> mechanism = credentials[0]

可能有用的其他信息是这些调用来自Python RQ工作程序。我试图在fork步骤之前建立连接,但是可能会发生一些事情导致这种情况发生。

(Pdb) os.getpid()
10507
... next query...
(Pdb) os.getpid()
10510

1 个答案:

答案 0 :(得分:3)

知道了!

默认的Python-RQ worker使用fork模型,而forking阻止了PyMongo共享连接套接字。

我切换到GeventWorker,现在默认共享套接字。