1

对于使用 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)

当我在验证函数中设置 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
4

1 回答 1

3

知道了!

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

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

于 2015-02-13T20:03:41.047 回答