MongoClient 在 fork 之前打开。仅创建 MongoClient Flask
MongoClient opened before fork. Create MongoClient only Flask
我是 运行 Flask,采用 uwsgi 线程模式,进程 4,还使用 pymongo flask_mongoengine,uwsgi 说 "MongoClient opened before fork. Create MongoClient only "
我尝试使用 connect=False 进行连接,但结果相同
lazy-apps = true 问题已解决,但 uwsgi 似乎需要更多时间来加载
怎样才能获得最佳性能?
app.config['MONGODB_SETTINGS'] = {'DB': 'somedb', "USERNAME":
"dbadmin", "PASSWORD":"somepass",'connect': False}
和
client = MongoClient(connect=False, username='dbadmin',
password='somepass', authSource='somedb')
Mongoengine 和 pymongo 的解决方案
如果您使用 appllication factory pattern then setting MongoClient connection=False
should fix it. It worked for my flask app (v1.0.2), running behind uwsgi server (v2.0.18).
例子
# __init__.py
from flask_mongoengine import MongoEngine
mongo = MongoEngine()
def create_app(config=None):
app = Flask(__name__)
app.config['MONGODB_HOST'] = 'localhost'
app.config['MONGODB_PORT'] = 27017
app.config['MONGODB_DB'] = 'datazzilla'
# NOTE: This fixes "UserWarning: MongoClient opened before fork."
# I'm not aware of side effects yet. Default value is/was "True"
app.config['MONGODB_CONNECT'] = False
mongo.init_app(app)
return app
我是 运行 Flask,采用 uwsgi 线程模式,进程 4,还使用 pymongo flask_mongoengine,uwsgi 说 "MongoClient opened before fork. Create MongoClient only " 我尝试使用 connect=False 进行连接,但结果相同
lazy-apps = true 问题已解决,但 uwsgi 似乎需要更多时间来加载 怎样才能获得最佳性能?
app.config['MONGODB_SETTINGS'] = {'DB': 'somedb', "USERNAME": "dbadmin", "PASSWORD":"somepass",'connect': False}
和
client = MongoClient(connect=False, username='dbadmin', password='somepass', authSource='somedb')
Mongoengine 和 pymongo 的解决方案
如果您使用 appllication factory pattern then setting MongoClient connection=False
should fix it. It worked for my flask app (v1.0.2), running behind uwsgi server (v2.0.18).
例子
# __init__.py
from flask_mongoengine import MongoEngine
mongo = MongoEngine()
def create_app(config=None):
app = Flask(__name__)
app.config['MONGODB_HOST'] = 'localhost'
app.config['MONGODB_PORT'] = 27017
app.config['MONGODB_DB'] = 'datazzilla'
# NOTE: This fixes "UserWarning: MongoClient opened before fork."
# I'm not aware of side effects yet. Default value is/was "True"
app.config['MONGODB_CONNECT'] = False
mongo.init_app(app)
return app