【问题标题】:web.py and gunicornweb.py 和 gunicorn
【发布时间】:2012-11-19 23:10:33
【问题描述】:

我的问题基本上是标题中的内容:如何设置 gunicorn 来运行 web.py 应用程序? (另外,如果有任何差异,我将如何在heroku上做到这一点?)

我已经使用内置的cherrypy在heroku上运行了我的应用程序,但是我无法让gunicorn与web.py一起使用(我只是不知道从哪里开始 - 我找不到任何教程) .

【问题讨论】:

    标签: python heroku web.py gunicorn


    【解决方案1】:

    恐怕我对 Heroku 不熟悉,但我可以回答你的基本问题。

    gunicorn 是一个 HTTP 服务器,用于通过 WSGI 运行 Python Web 应用程序。 web.py 是一个使用 WSGI 创建 Python Web 应用程序的框架。

    因此,您实际上并不需要一起使用这两种方法的教程,因为您需要做的就是弄清楚如何将 web.py 应用程序的 WSGI 入口点传递给 gunicorn。 WSGI 应用程序只是一个具有正确接口的 Python 可调用程序,即它采用某些参数并返回某个响应。请参阅this WSGI tutorial 了解更多信息。

    web.py 教程中的“hello world”应用程序看起来像这个 test.py:

    import web
    
    urls = (
        '/', 'index'
    )
    
    class index:
        def GET(self):
            return "Hello, world!"
    
    if __name__ == "__main__":
        app = web.application(urls, globals())
        app.run()
    

    但这并没有暴露 gunicorn 需要的 WSGI 应用程序。

    web.py 通过web.applicationwsgifunc 方法提供了一个WSGI 应用程序。我们可以通过在 index 类之后添加以下内容来将其添加到 test.py:

    # For serving using any wsgi server
    wsgi_app = web.application(urls, globals()).wsgifunc()
    

    这基本上是 web.py 文档在部署部分告诉你在使用Apache + mod_wsgi 时要做的事情——Python 代码对我们来说与 gunicorn 相同的事实并非巧合,因为这正是 WSGI为您提供 - 一种编写 Python 的标准方法,以便可以使用任何支持 WSGI 的服务器进行部署。

    gunicorn docs 中所述,我们可以将gunicorn 指向test 模块的wsgi_app 成员,如下所示:

    (tmp)day@office:~/tmp$ gunicorn test:wsgi_app
    2012-12-03 23:31:11 [19265] [INFO] Starting gunicorn 0.16.1
    2012-12-03 23:31:11 [19265] [INFO] Listening at: http://127.0.0.1:8000 (19265)
    2012-12-03 23:31:11 [19265] [INFO] Using worker: sync
    2012-12-03 23:31:11 [19268] [INFO] Booting worker with pid: 19268
    

    【讨论】:

    • 谢谢,我只是没有意识到您需要来自 web.application 的 wsgi_app 变量。出于某种原因,我虽然必须定义自己的函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2011-08-07
    • 1970-01-01
    • 2016-04-24
    • 2012-08-17
    • 2016-10-15
    • 2011-05-30
    相关资源
    最近更新 更多