MongoClient在派生之前打开。仅创建MongoClient烧瓶

时间:2018-07-02 15:05:24

标签: flask pymongo uwsgi flask-mongoengine

我正在使用进程4的uwsgi线程模式运行Flask,还使用pymongo flask_mongoengine和uwsgi说“ MongoClient在fork之前打开。仅创建MongoClient” 我尝试用connect = False连接,但结果相同

lazy-apps = true问题已解决,但似乎uwsgi需要更多时间来加载 怎样才能获得最佳性能?

2 个答案:

答案 0 :(得分:3)

  

app.config ['MONGODB_SETTINGS'] = {'DB':'somedb',“ USERNAME”:   “ dbadmin”,“ PASSWORD”:“ somepass”,“ connect”:False}

  

client = MongoClient(connect = False,username ='dbadmin',   password ='somepass',authSource ='somedb')

Mongoengine和pymongo的解决方案

答案 1 :(得分:1)

如果您使用appllication factory pattern,则设置 MongoClient connection=False应该可以解决。它适用于我的烧瓶应用(v1.0.2),在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