Sadme Rame
Sadme Rame

Reputation: 429

MongoClient opened before fork. Create MongoClient only Flask

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 ?

Upvotes: 5

Views: 9662

Answers (2)

Lukasz Dynowski
Lukasz Dynowski

Reputation: 13630

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

Upvotes: 6

Sadme Rame
Sadme Rame

Reputation: 429

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

Upvotes: 9

Related Questions