简体   繁体   中英

Can't run tornado app using gunicorn

I can't run tornado application using gunicorn. There is an error while startup app. I want to run it using gunicorn because I need some good features like: graceful-timeout, response-timeout and etc...

tornado app:

$cat wsgi.py

source code:

import tornado.web
import tornado.wsgi
from api.handler import MainHandler, ApiV2Handler, InvalidRequestHandler


def app(*args):
    app = tornado.web.Application([
        (r"/", MainHandler),
        (r"(/v3/(\w+)/(\w+)/)", ApiV2Handler),
        (r"(/v3/(\w+)/(\w+))", InvalidRequestHandler)
    ])
    return tornado.wsgi.WSGIContainer(tornado.wsgi.WSGIAdapter(app))

bash:

$ gunicorn wsgi:app --bind 127.0.0.1:9080

traceback:

[2015-07-06 14:41:16 +0000] [21806] [INFO] Starting gunicorn 19.3.0
[2015-07-06 14:41:16 +0000] [21806] [INFO] Listening at: http://127.0.0.1:9080 (21806)
[2015-07-06 14:41:16 +0000] [21806] [INFO] Using worker: sync
[2015-07-06 14:41:16 +0000] [21811] [INFO] Booting worker with pid: 21811
[2015-07-06 14:41:21 +0000] [21811] [ERROR] Error handling request
Traceback (most recent call last):
  File "venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
    self.handle_request(listener, req, client, addr)
  File "venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
    for item in respiter:
TypeError: 'WSGIContainer' object is not iterable
^C[2015-07-06 14:41:23 +0000] [21806] [INFO] Handling signal: int
[2015-07-06 14:41:23 +0000] [21811] [INFO] Worker exiting (pid: 21811)
[2015-07-06 14:41:23 +0000] [21806] [INFO] Shutting down: Master

Any ideas?

Update for Ben Darnell:

I tried this:

import tornado.web
import tornado.wsgi
from api.handler import MainHandler, ApiV2Handler, InvalidRequestHandler


def app(*args):
    app = tornado.web.Application([
        (r"/", MainHandler),
        (r"(/v3/(\w+)/(\w+)/)", ApiV2Handler),
        (r"(/v3/(\w+)/(\w+))", InvalidRequestHandler)
    ])
    return tornado.wsgi.WSGIAdapter(app)

But result is the same:

TypeError: 'WSGIAdapter' object is not iterable

works for me:

gunicorn -k tornado wsgi:app

wsgi.py

import tornado.web
import tornado.wsgi
from api.handler import MainHandler, ApiV2Handler, InvalidRequestHandler

app = tornado.web.Application([
    (r"/", MainHandler),
    (r"(/v3/(\w+)/(\w+)/)", ApiV2Handler),
    (r"(/v3/(\w+)/(\w+))", InvalidRequestHandler)
])

good luck!

To run in WSGI mode, return the WSGIAdapter; do not use a WSGIContainer (WSGIContainer is for going the other direction, when you have a WSGI application to run on a tornado server). However, gunicorn has a native tornado mode so you will probably get better results in this mode than using WSGI.

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