Code Monkey home page Code Monkey logo

Comments (6)

higee avatar higee commented on May 22, 2024 1

Thanks for the comment!! I'll go over the links you've attached! And since I'm done with this specific issue, I think it's better to close it.

from uwsgi-nginx-flask-docker.

 avatar commented on May 22, 2024 1

If it can help, I solved by replace :

from flask.ext.mongoengine import MongoEngine

by :

from flask_mongoengine import MongoEngine

from uwsgi-nginx-flask-docker.

tiangolo avatar tiangolo commented on May 22, 2024

Here's how uWSGI is configured, from the base image: https://github.com/tiangolo/uwsgi-nginx-docker

2016-08-16: Use dynamic a number of worker processes for uWSGI, from 2 to 16 depending on load. This should work for most cases. This helps especially when there are some responses that are slow and take some time to be generated, this change allows all the other responses to keep fast (in a new process) without having to wait for the first (slow) one to finish.


Now, what are you trying to achieve exactly? What do you want the lazy-apps for?

from uwsgi-nginx-flask-docker.

higee avatar higee commented on May 22, 2024

Here's my issue in detail. Bottom line is that I'm saving data to mongodb using flask-pymongo but flask-pymongo gives me a warning that says:

MongoClient opened before fore. Create MongoClient only after forking. 
See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#is-pymongo-fork-safe

So while searching a way to solve this, I found this issue in flask-pymongo repository. In the middle of the thread, there is a comment from hut8 that says:


@dkiser Out of curiosity, what app server are you using? I am using uwsgi. I just spent like two hours with a bug that I think is the same. Mine never throws an exception but instead just hangs, and this is what I get in the logs.

announcing my loyalty to the Emperor...
/srv/venv/lib/python3.4/site-packages/pymongo/topology.py:74: 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#using-pymongo-with-multiprocessing>
  "MongoClient opened before fork. Create MongoClient "
[pid: 15145|app: 0|req: 1/2]  1.2.3.4 () {46 vars in 753 bytes} [Sun Jan 24 01:54:21 2016] GET /stats => generated 39 bytes in 30023 msecs (HTTP/1.1 500) 2 headers in 90 bytes (1 switches on core 0)
There's a timeout at 30 seconds. I can connect just fine using exactly the same stack/virtualenv (minus the app server of course) using a Flask-Script shell (https://flask-script.readthedocs.org/en/latest/#shell).

My solution was a one line fix; just add lazy-apps = true to my uwsgi INI file. That makes the workers fork then load the application.


So I was trying to test his idea of setting lazy-apps=true inside uwsgi.ini.
Following is some blocks of code to help you grasp what I was trying to do.

main.py

from flask import Flask
from flask_restful import Api
from flask_pymongo import PyMongo
from resources.test import Test

app = Flask(__name__)
app.config['MONGO_DBNAME'] = ''
app.config['MONGO_URI'] = ''
app.mongo = Pymongo(app, config_prefix='MONGO')

api = Api(app)
api.add_resource(Test, '/test')

if __name__ == '__main__':
    app.run(port=7000)

resources/test.py

from flask_restful import Resource, reqparse
from flask import current_app
 
class Test(Resource):
 
    parser = reqparse.RequestParser()
 
    parser.add_argument('name', type=str)
    parser.add_argument('age', type=int)
 
    def post(self):
 
        data = Test.parser.parse_args() 
        response  = {
            'name' : data['name'], 
            'age' : data['age']
        }
 
        current_app.mongo.db.test_collection.insert_one(response)        
 
        return {'message' : 'successfully inserted'}

from uwsgi-nginx-flask-docker.

higee avatar higee commented on May 22, 2024

I changed my code a bit and it gives me the same warning only once.
So I guess I no longer have to change uwsgi.ini for this issue.

main.py

from flask import Flask
from flask_restful import Api
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config['MONGO_DBNAME'] = ''
app.config['MONGO_URI'] = ''
app.mongo = Pymongo(app, config_prefix='MONGO')

from resources.test import Test

api = Api(app)
api.add_resource(Test, '/test')

if __name__ == '__main__':
    app.run(port=7000)

resources/test.py

from flask_restful import Resource, reqparse
from main import mongo
 
class Test(Resource):
 
    parser = reqparse.RequestParser()
 
    parser.add_argument('name', type=str)
    parser.add_argument('age', type=int)
 
    def post(self):
 
        data = Test.parser.parse_args() 
        response  = {
            'name' : data['name'], 
            'age' : data['age']
        }
 
        mongo.db.test_collection.insert_one(response)        
 
        return {'message' : 'successfully inserted'}

from uwsgi-nginx-flask-docker.

tiangolo avatar tiangolo commented on May 22, 2024

Cool! It seems you solved your issue. šŸŽ‰


If by any chance you need to modify and debug it more, it might help you to know that this image bases itself on another image with just uwsgi-nginx and it has two uwsgi.ini files at the same time. To allow having a general one with all the default configurations and another one with the specific app configuration, without all the noise, to ease development.

Here's where the first file is copied in the Dockerfile: https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/python3.6/Dockerfile#L110

Here's where the second file is copied in the Dockerfile: https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/python3.6/Dockerfile#L142

Here's the first (default, global) uwsgi.ini file: https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/python3.6/uwsgi.ini

Here's the original base app specific uwsgi.ini: https://github.com/tiangolo/uwsgi-nginx-docker/blob/master/python3.6/app/uwsgi.ini

This image replaces that base app specific uwsgi.ini with: https://github.com/tiangolo/uwsgi-nginx-flask-docker/blob/master/python3.6/app/uwsgi.ini

from uwsgi-nginx-flask-docker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.