Flask 应用程序无法在 dockerized 设置中使用 mongoengine 从 mongodb 查询数据
Flask app unable to query data from mongodb using mongoengine in a dockerized setup
我是 运行 一个网站应用程序,我正在使用 Flask,它使用 mongoengine 查询 mongodb 数据库。当应用程序尝试查询数据库时出现错误。
我在其中一个容器中安装了 mongodb,我可以成功连接到它。
mongodb: 2019-09-06T03:45:11.801+0000 I NETWORK [conn1] received client metadata from 172.20.0.3:43918 conn1: { driver: { name: "PyMongo", version: "3.9.0" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "3.10.0-957.27.2.el7.x86_64" }, platform: "CPython 3.7.2.final.0" }
Docker 设置
version: "3.7"
services:
portfolio:
build: ./portfolio
container_name: portfolio
restart: always
environment:
- APP_NAME=portfolio
expose:
- 8080
links:
- mongodb
admin:
build: ./admin
container_name: admin
restart: always
environment:
- APP_NAME=admin
expose:
- 8089
links:
- mongodb
mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
nginx:
build: ./nginx
depends_on:
- portfolio
- admin
container_name: nginx
restart: always
ports:
- "80:80"
Python 设置:
from flask import Flask
from mongoengine import connect
app = Flask(__name__)
connect('spees_db', host='mongodb://mongodb:27017/spees_db')
我做的时候出错
email_ex = User.objects(email=email).first()
错误堆栈:
nginx | 102.140.206.140 - - [06/Sep/2019:03:53:09 +0000] "GET /register HTTP/1.1" 200 4145 "http://admin.shemoirere.com/login?next=%2F" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
[pid: 13|app: 0|req: 4/5] 102.140.206.140 () {44 vars in 967 bytes} [Fri Sep 6 03:53:09 2019] GET /register => generated 4145 bytes in 10 msecs (HTTP/1.1 200) 4 headers in 192 bytes (1 switches on core 1)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./admin/views.py", line 37, in do_register
email_ex = User.objects(email=email).first()
File "/usr/local/lib/python3.7/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 207, in _get_collection
db.client.is_primary:
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1006, in is_primary
return self._server_property('is_writable')
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 831, in _server_property
writable_server_selector)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 231, in select_server
nginx | 2019/09/06 03:53:50 [error] 6#6: *5 upstream prematurely closed connection while reading response header from upstream, client: 102.140.206.140, server: admin.shemoirere.com, request: "POST /register HTTP/1.1", upstream: "uwsgi://172.20.0.4:8089", host: "admin.shemoirere.com", referrer: "http://admin.shemoirere.com/register"
nginx | 102.140.206.140 - - [06/Sep/2019:03:53:50 +0000] "POST /register HTTP/1.1" 502 559 "http://admin.shemoirere.com/register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
address))
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 189, in select_servers
selector, server_timeout, address)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 205, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
我仔细研究了您的存储库并解决了您的问题。这是你的 wsgi 配置-
[uwsgi]
wsgi-file = run.py
可调用=应用程序
插座=:8089
进程= 4
线程 = 2
主人=真
chmod-socket = 660
真空 = 真
die-on-term = 真
问题出在 processes
参数中。 uwsgi 启动一个主进程,然后使用 fork() 生成工作进程。你在初始启动期间正在做你的 connect
- 所以连接存在于你的主进程中,而你的 children 得到了一个错误的副本。当连接发生时,您实际上可以在 mongo 上看到日志...只有 1! pymongo 特别指出 fork() 是不安全的连接。
最简单的修复方法是将 lazy-apps = true
添加到该 wsgi 文件的末尾。然后系统按预期工作。您还可以设置您的应用程序,以避免在第一次需要它之前实际调用 connect
(可能是第一次请求挂钩之前?)
https://api.mongodb.com/python/3.9.0/faq.html#is-pymongo-fork-safe
我是 运行 一个网站应用程序,我正在使用 Flask,它使用 mongoengine 查询 mongodb 数据库。当应用程序尝试查询数据库时出现错误。
我在其中一个容器中安装了 mongodb,我可以成功连接到它。
mongodb: 2019-09-06T03:45:11.801+0000 I NETWORK [conn1] received client metadata from 172.20.0.3:43918 conn1: { driver: { name: "PyMongo", version: "3.9.0" }, os: { type: "Linux", name: "Linux", architecture: "x86_64", version: "3.10.0-957.27.2.el7.x86_64" }, platform: "CPython 3.7.2.final.0" }
Docker 设置
version: "3.7"
services:
portfolio:
build: ./portfolio
container_name: portfolio
restart: always
environment:
- APP_NAME=portfolio
expose:
- 8080
links:
- mongodb
admin:
build: ./admin
container_name: admin
restart: always
environment:
- APP_NAME=admin
expose:
- 8089
links:
- mongodb
mongodb:
image: mongo
container_name: mongodb
ports:
- "27017:27017"
nginx:
build: ./nginx
depends_on:
- portfolio
- admin
container_name: nginx
restart: always
ports:
- "80:80"
Python 设置:
from flask import Flask
from mongoengine import connect
app = Flask(__name__)
connect('spees_db', host='mongodb://mongodb:27017/spees_db')
我做的时候出错
email_ex = User.objects(email=email).first()
错误堆栈:
nginx | 102.140.206.140 - - [06/Sep/2019:03:53:09 +0000] "GET /register HTTP/1.1" 200 4145 "http://admin.shemoirere.com/login?next=%2F" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
[pid: 13|app: 0|req: 4/5] 102.140.206.140 () {44 vars in 967 bytes} [Fri Sep 6 03:53:09 2019] GET /register => generated 4145 bytes in 10 msecs (HTTP/1.1 200) 4 headers in 192 bytes (1 switches on core 1)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./admin/views.py", line 37, in do_register
email_ex = User.objects(email=email).first()
File "/usr/local/lib/python3.7/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 207, in _get_collection
db.client.is_primary:
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1006, in is_primary
return self._server_property('is_writable')
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 831, in _server_property
writable_server_selector)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 231, in select_server
nginx | 2019/09/06 03:53:50 [error] 6#6: *5 upstream prematurely closed connection while reading response header from upstream, client: 102.140.206.140, server: admin.shemoirere.com, request: "POST /register HTTP/1.1", upstream: "uwsgi://172.20.0.4:8089", host: "admin.shemoirere.com", referrer: "http://admin.shemoirere.com/register"
nginx | 102.140.206.140 - - [06/Sep/2019:03:53:50 +0000] "POST /register HTTP/1.1" 502 559 "http://admin.shemoirere.com/register" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
address))
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 189, in select_servers
selector, server_timeout, address)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 205, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
我仔细研究了您的存储库并解决了您的问题。这是你的 wsgi 配置-
[uwsgi] wsgi-file = run.py 可调用=应用程序 插座=:8089 进程= 4 线程 = 2 主人=真 chmod-socket = 660 真空 = 真 die-on-term = 真
问题出在 processes
参数中。 uwsgi 启动一个主进程,然后使用 fork() 生成工作进程。你在初始启动期间正在做你的 connect
- 所以连接存在于你的主进程中,而你的 children 得到了一个错误的副本。当连接发生时,您实际上可以在 mongo 上看到日志...只有 1! pymongo 特别指出 fork() 是不安全的连接。
最简单的修复方法是将 lazy-apps = true
添加到该 wsgi 文件的末尾。然后系统按预期工作。您还可以设置您的应用程序,以避免在第一次需要它之前实际调用 connect
(可能是第一次请求挂钩之前?)
https://api.mongodb.com/python/3.9.0/faq.html#is-pymongo-fork-safe