• Mongo错误记录:MongoClient opened before fork. Create MongoClient


    mongodb+pool多进程

    在启动时会有如下提示:

    /home/kk/python3.6_kk/lib/python3.6/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe>
    "MongoClient opened before fork. Create MongoClient "
    /home/kk/python3.6_kk/lib/python3.6/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe>
    "MongoClient opened before fork. Create MongoClient "
    /home/kk/python3.6_kk/lib/python3.6/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe>
    "MongoClient opened before fork. Create MongoClient "
    /home/kk/python3.6_kk/lib/python3.6/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe>
    "MongoClient opened before fork. Create MongoClient "

    "MongoClient opened before fork. Create MongoClient "

    只需要在数据库连接时  加上,connect=False

    client=pymongo.MongoClient('localhost',27017,connect=False)

    配合flask和gunicorn使用报错

    使用pymongo配合flask和gunicorn使用的时候触发以下错误信息:

    [CRITICAL] WORKER TIMEOUT 
    /home/for4c/projects/excel.d/venv3.4/lib/python3.4/site-packages/pymongo/topology.py:145: UserWarning: MongoClient opened before fork. 
    Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#pymongo-fork-safe> "MongoClient opened before fork. Create MongoClient " [2017-09-19 15:20:10 +0800] [25896] [INFO] Worker exiting (pid: 25896)

     原因解释

    提示的意思是MongoClient在fork之前开启了,官方文档指出:

    PyMongo is not fork-safe. Care must be taken when using instances of MongoClient with fork(). Specifically, instances of MongoClient must not be copied from a parent process to a child process.

    fork是启动新进程的方法,并且由于PyMongo不是进程安全的,所以不应该将该实例从父进程中复制到子进程当中。在这个flask应用中,flask使用gunicorn作为网关接口,在启动的时候会启动一个主进程和多个子进程,也就是master/workers,这个时候就出现了MongoClient实例在进程之间的传递。

    为了解决这个问题,在实例化MongoClient对象的时候也要加上connect=False参数。

    app.mongo = MongoClient(host='0.0.0.0',port=27017,connect=False)

    完结~~

    大多数人都以为是才智成就了科学家,他们错了,是品格。---爱因斯坦
  • 相关阅读:
    .NET中使用Memcached的相关资源整理
    Windows 下的.NET+ Memcached安装
    基于.NET的大型Web站点StackOverflow架构分析(转)
    组建学习型项目团队(转)
    WIN 2003服务器终极安全及问题解决方案
    禁止用户远程登录方法方法
    微信公众平台完整开发教程【转】
    【转】Android 最火的快速开发框架XUtils
    【转】Android 最火框架XUtils之注解机制详解
    android开发Proguard混淆与反射
  • 原文地址:https://www.cnblogs.com/wqbin/p/14666797.html
Copyright © 2020-2023  润新知