gunicorn、Flask 和 PyMongo 间歇性超时失败

问题描述 投票:0回答:1

我有在AWS ECS容器上运行的gunicorn。 Gunicorn 服务请求背后有 Flask 应用程序。 Flask 应用程序使用 pymongo 与 AWS dcoumentDB 进行交互。这在大多数情况下都有效,但我从 pymongo 得到“随机失败”。这是我运行 find_one() 查询时得到的引用。请注意,相同的查询几乎有十分之九有效。我现在是 API 的唯一用户。所以不是交通繁忙的问题。 File "/opt/lib/python3.8/site-packages/pymongo/collection.py", line 1452, in find_one for result in cursor.limit(-1): File "/opt/lib/python3.8/site-packages/pymongo/cursor.py", line 1248, in next if len(self.__data) or self._refresh(): File "/opt//lib/python3.8/site-packages/pymongo/cursor.py", line 1139, in _refresh self.__session = self.__collection.database.client._ensure_session() File "/opt/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1712, in _ensure_session return self.__start_session(True, causal_consistency=False) File "/opt/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1657, in __start_session self._topology._check_implicit_session_support() File "/opt/lib/python3.8/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support self._check_session_support() File "/opt/lib/python3.8/site-packages/pymongo/topology.py", line 554, in _check_session_support self._select_servers_loop( File "/opt/lib/python3.8/site-packages/pymongo/topology.py", line 238, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: <document_db_cluster_host>:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 66340edd96390975a24866e3, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('<document_db_cluster_host>', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('<document_db_cluster_host>:27017: timed out')>]>',)"

有人遇到过类似的问题吗?

按照建议,我在创建 MongoClient 时使用 connect=False 。我还确保在 Flask 应用程序完全初始化并收到第一个请求后实例化 MongoClient。我在创建 MongoClient 时尝试了各种组合,例如 readPreference="primaryPreferred"、directConnection=True(以主机作为主实例)和 maxPoolSize=1。我还简化了 Gunicorn 配置,因此它仅使用 1 个工作线程和 1 个线程。但没有任何效果。

flask pymongo gunicorn aws-documentdb-mongoapi
1个回答
0
投票

© www.soinside.com 2019 - 2024. All rights reserved.