python - web.py 和 gunicorn

标签 python heroku web.py gunicorn

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

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

最佳答案

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

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

因此,您实际上并不需要将两者结合使用的教程,因为您需要做的就是弄清楚如何将 web.py 应用程序的 WSGI 入口点传递给 gunicorn。 WSGI 应用程序只是一个具有正确接口(interface)的 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

关于python - web.py 和 gunicorn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13667103/

相关文章:

ruby-on-rails - Heroku 无法识别 'spring' gem,即使它列在 Gemfile 中

heroku - 将现有 Docker 镜像部署到 Heroku

python - 无法在 web.py 中创建 ssl session - Python 2.7

python - 在二维数组内追加/连接数组

python - 将 Matlab 代码翻译成 Numpy

ruby-on-rails - 回形针:从带有扩展名的 url 上传

javascript - jquery.post() 后端响应,但回调函数参数为空

python - 使用 web.py 的 web.database 和 MyISAM

Python 赋值给条件 LHS

python - except block 未捕获 Cloud firestore 异常