简体   繁体   English

web.py和gunicorn

[英]web.py and gunicorn

My question is basically what's in the title: how can I setup gunicorn to run a web.py app? 我的问题基本上是标题中的内容:如何设置gunicorn来运行web.py应用程序? (Also, if there are any differences, how would I do it on heroku?) (另外,如果有任何差异,我将如何在heroku上进行?)

I already have my app running on heroku using the built in cherrypy, but I have not been able to get gunicorn to work with web.py (I just have no idea where to start - I couldn't find any tutorials). 我已经使用内置的cherrypy在heroku上运行了我的应用程序,但是我无法使用gun.orn来使用web.py(我根本不知道从哪里开始 - 我找不到任何教程)。

I'm afraid I'm not familar with Heroku, but I can answer your basic question. 我担心我不熟悉Heroku,但我可以回答你的基本问题。

gunicorn is a HTTP server for running Python web apps via WSGI. gunicorn是一个HTTP服务器,用于通过WSGI运行Python Web应用程序。 web.py is a framework for creating Python web apps using WSGI. web.py是一个使用WSGI创建Python Web应用程序的框架。

So you don't really need a tutorial for using both together, as all you need to do is figure out how to pass the WSGI entry point of your web.py application to gunicorn. 因此,您并不需要一起使用这两者的教程,因为您需要做的就是弄清楚如何将web.py应用程序的WSGI入口点传递给gunicorn。 A WSGI application is just a Python callable with the right interface ie it takes certain parameters and returns a certain response. WSGI应用程序只是一个Python可调用,具有正确的接口,即它需要某些参数并返回一定的响应。 See this WSGI tutorial for more. 有关更多信息,请参阅此WSGI教程

The "hello world" application from the web.py tutorial looks like this test.py: 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()

But that does not expose the WSGI application which gunicorn needs. 但这并没有暴露出gunicorn需要的WSGI应用程序。

web.py provides a WSGI application via the wsgifunc method of web.application . web.py提供经由一个WSGI应用wsgifunc的方法web.application We can add this to test.py by adding the following after the index class: 我们可以通过在index类之后添加以下内容将其添加到test.py:

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

This is basically what the web.py documentation tells you to do in the deployment section, when using Apache + mod_wsgi - the fact that the Python code is the same for us with gunicorn is not a coincidence because this is exactly what WSGI gives you - a standard way to write the Python so that it can be deployed using any WSGI capable server. 这基本上是web.py文档告诉你在使用Apache + mod_wsgi时在部署​​部分中做的事情 - 事实上Python代码与gunicorn相同并不是巧合,因为这正是WSGI给你的 -编写Python的标准方法,以便可以使用任何支持WSGI的服务器进行部署。

As explained in the gunicorn docs , we can then point gunicorn at the wsgi_app member of the test module as follows: 正如gunicorn文档中所解释的那样 ,我们可以将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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