问题
I am running Flask with uwsgi threaded mode with processes 4 and using pymongo also flask_mongoengine and uwsgi says "MongoClient opened before fork. Create MongoClient only " I tried connect with connect=False but the result is same
lazy-apps = true problem is fixed but it seems that uwsgi needs more time to load what can be done for best performance ?
回答1:
app.config['MONGODB_SETTINGS'] = {'DB': 'somedb', "USERNAME": "dbadmin", "PASSWORD":"somepass",'connect': False}
And
client = MongoClient(connect=False, username='dbadmin', password='somepass', authSource='somedb')
Solutions for Mongoengine and For pymongo
回答2:
If you use 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).
Example
# __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
来源:https://stackoverflow.com/questions/51139089/mongoclient-opened-before-fork-create-mongoclient-only-flask