MongoClient opened before fork. Create MongoClient only Flask

半世苍凉 提交于 2019-12-20 05:12:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!