MongoClient opened before fork. Create MongoClient only Flask

女生的网名这么多〃 提交于 2019-12-02 06:50:31

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

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