Reputation: 11
I have a sample Flask-RestPlus application as myapp.py:
from flask import Flask
from flask_restplus import Resource, API
API = Api(version='1.0', title='my_routes')
MY_NAMESPACE = API.namespace('v1.0/routes', description='v1.0 routes')
def initialize():
app = Flask(__name__)
API.init_app(app)
API.add_namespace(MY_NAMESPACE)
return app
@MY_NAMESPACE.route('/hello')
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
if __name__ == '__main__':
MY_ROUTE = initialize()
MY_ROUTE.run(host='0.0.0.0', port=8080)
This works fine if I am running python myapp.py. Since I want to run to run this code on production, I want to use Gunicorn. I have gunicorn installed on my UNIX machine. Sample code from https://gunicorn.org/ works perfectly fine. How do I run code in my case? I tried:
gunicorn -w 4 myapp:initialize
gunicorn -w 4 "myapp:initialize()"
gunicorn -w 4 myapp:main
But none of them worked. Do I have to make some changes in code?
Upvotes: 0
Views: 451
Reputation: 3506
In your myapp.py, turn your part:
if __name__ == '__main__':
MY_ROUTE = initialize()
MY_ROUTE.run(host='0.0.0.0', port=8080)
into:
app = initialize() # app has to be accessible outside if
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
and then run
gunicorn --bind 0.0.0.0:8080 --workers 4 myapp:app --log-level debug
if you want to run in production mode, specify --log-level info
.
Upvotes: 1