【问题标题】:Can't run tornado app using gunicorn无法使用 gunicorn 运行龙卷风应用程序
【发布时间】:2015-09-23 13:34:04
【问题描述】:

我无法使用 gunicorn 运行 tornado 应用程序。启动应用程序时出错。我想使用 gunicorn 运行它,因为我需要一些好的功能,例如:优雅超时、响应超时等......

龙卷风应用:

$cat wsgi.py

源代码:

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))

重击:

$ gunicorn wsgi:app --bind 127.0.0.1:9080

追溯:

[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

有什么想法吗?

更新本·达内尔:

我试过这个:

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)

但结果是一样的:

TypeError: 'WSGIAdapter' object is not iterable

【问题讨论】:

  • 看起来 gunicorn 期望 app 成为 WSGIAdapter 对象,而不是返回 WSGI 应用程序的函数。

标签: python python-2.7 tornado wsgi gunicorn


【解决方案1】:

要在 WSGI 模式下运行,返回 WSGIAdapter;不要使用 WSGIContainer(当你有一个 WSGI 应用程序要在龙卷风服务器上运行时,WSGIContainer 是用于另一个方向)。但是,gunicorn 有一个原生的龙卷风模式,所以在这种模式下你可能会比使用 WSGI 获得更好的结果。

【讨论】:

    【解决方案2】:

    为我工作:

    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)
    ])
    

    祝你好运!

    【讨论】:

    • 这是否允许 websockets?
    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多