Using Python Flask-restful with mod-wsgi
Question: I am trying to use mod-wsgi with Apache 2.2

I have the following directory structure:

    
    
    scheduling-algos
    -lib
    -common
     -config
      -config.json
    resources
    -Optimization.py
    optimization.wsgi
    optimization_app.py
    

My `optimization_app.py` is the following:

    
    
    from flask import Flask
    from flask_restful import Api
    from resources.Optimization import OptimizationAlgo
    
    def optimizeInstances():
        optimization_app = Flask(__name__)
        api = Api(optimization_app)
        api.add_resource(OptimizationAlgo, '/instances')
    
    if __name__ == '__main__':
        optimizeInstances()
        optimization_app.run(host='0.0.0.0', debug=True)
    

My `Optimization.py` code looks like the following:

    
    
    class OptimizationAlgo(Resource):
        def post(self):
           return "success"
    

When I make a `POST` request to the url `http://<host>:5000/instances`, it
works just as expected. I want make this work using `WSGI`. I have `mod-wsgi`
installed with Apache 2.2.

My `optimization.wsgi` file looks like the following

    
    
    import sys
    sys.path.insert(0, '<path to app>')
    
    from optimization_app import optimizeInstances as application
    

I get the following error: `TypeError: optimizeInstances() takes no arguments
(2 given)` . Apparently this is not the correct way to use WSGI. What is the
correct way to use WSGI? Apparently, this is not the correct way to use
`WSGI`.


Answer: As I told you in your other question, you should perhaps go back and read the
Flask [documentation](http://flask.pocoo.org) again. That way you will learn
and understand properly. By ignoring advice and expecting others to tell you,
it only annoys people and they will stop helping you. Would suggest you take
heed of that rather than leave a trail of separate questions hoping someone
will solve your problems for you.

That said, I can't see how the code you give can even work with the Flask
development server as you claim. The problem is that `optimization_app =
Flask(__name__)` is setting a local variable within function scope. It isn't
setting a global variable. As a result the call of
`optimization_app.run(host='0.0.0.0', debug=True)` should fail with a
`LookupError` as it will not see a variable called `optimization_app`. Not
even sure why you are bothering with the function.

If you go look at the Flask documentation, the pattern it would likely use is:

    
    
    # optimisation.wsgi
    
    import sys
    sys.path.insert(0, '<path to app>')
    
    # We alias 'app' to 'application' here as mod_wsgi expects it to be called 'application'.
    
    from optimization_app import app as application
    
    # optimization_app.py
    
    from flask import Flask
    from flask_restful import Api
    from resources.Optimization import OptimizationAlgo
    
    app = Flask(__name__)
    
    api = Api(app)
    api.add_resource(OptimizationAlgo, '/instances')
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    

