升级到PyMongo 3.0导致ServerSelectionTimeoutError

Jua*_*rah 9 python nginx mongodb pymongo uwsgi

我最近将Bottle + uWSGI + Nginx应用程序升级到MongoDB 3.0.2.它与PyMongo 2.8一起工作正常,但今天我通过运行以下命令升级到PyMongo 3.0:

pip install --upgrade pymongo
Run Code Online (Sandbox Code Playgroud)

我没有对代码进行任何其他更改,但现在我不断收到以下错误.

File "/pymongo/cursor.py", line 968, in __next__ if len(self.__data) or self._refresh():
File "/pymongo/cursor.py", line 905, in _refresh self.__read_preference))
File "/pymongo/cursor.py", line 812, in __send_message **kwargs)
File "/pymongo/mongo_client.py", line 716, in _send_message_with_response server = topology.select_server(selector)
File "/pymongo/topology.py", line 113, in select_server server_selection_timeout))
File "/pymongo/topology.py", line 93, in select_servers self._error_message(selector))
ServerSelectionTimeoutError: No servers found yet
Run Code Online (Sandbox Code Playgroud)

我用来连接数据库的功能如下:

def connect_db(db_name):
    global db
    host = "localhost"
    port = 27017
    connection = pymongo.MongoClient(host=host, port=port)
    db = connection[db_name]
Run Code Online (Sandbox Code Playgroud)

我重新启动了所有服务器.静态页面工作正常,但任何试图访问数据库的页面都会挂起并抛出上面的错误.但是,如果我转到mongoshell或Python shell并查询MongoDB服务器,它可以正常工作.

>>> import pymongo
>>> host = "localhost"
>>> port = 27017
>>> connection = pymongo.MongoClient(host=host, port=port)
>>> db = connection[test]
>>> db.test.insert_one({"test": True});
<pymongo.results.InsertOneResult object at 0x7fc43b8efc80>
Run Code Online (Sandbox Code Playgroud)

似乎只有我的应用程序找不到MongoDB服务器.请注意,我正在使用虚拟环境,以防以任何方式影响情况.此外,如果我降级回PyMongo 2.8,一切正常.

Nic*_*cci -1

这是否与使用 mongos 时的关键 PyMomongo 3.0 错误有关?如果是这样,他们会在 master 分支上推送修复程序(请参阅此提交。)