简体   繁体   中英

How to run Flask app using gunicorn?

The question can be devided on two separate parts.

  1. I have the following project stucture

     |- project | |-- app/ - directory with actual project code | |-- app.py - imports something from app/ and call create_app 

    When I run gunicorn I should point him to app object which is actually created in app.py . So I get an error because gunicorn treats app:app as a package. The only way is to rename something?

  2. I use factory method to create app. So I import create_app fuction in app.py and pass it to Manager from flask.ext.script . I pass manager object to gunicorn. In this case gunicorn runs correctly but once the first request comes I get the following error:

     [2015-03-25 15:38:11 +0000] [14395] [ERROR] Error handling request Traceback (most recent call last): File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 52, in handle self.handle_request(listener_name, req, client, addr) File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 159, in handle_request super(GeventWorker, self).handle_request(*args) File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 105, in handle_request respiter = self.wsgi(environ, resp.start_response) TypeError: __call__() takes at most 2 arguments (3 given) 

    Perhaps I could create some file wsgi.py and provide current app for gunicorn ?

You will have issues if two things named "app" are in the same directory in Python's path.

You need to pass a Flask app instance directly to gunicorn. The command line manager is not a WSGI app, which is why you get that error.

You can simply point gunicorn directly at a call to your app factory, there is no need for any intermediate code.

gunicorn app:create_app()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM