本文介绍了web.py和gunicorn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的问题基本上是标题中的内容:如何设置gunicorn来运行web.py应用程序? (另外,如果有任何区别,我将如何在heroku上做到这一点?) 我已经使用内置的cherrypy在heroku上运行我的应用程序,但是我有没有能够得到gunicorn与web.py工作(我只是不知道从哪里开始 - 我找不到任何教程)。 解决方案恐怕我不熟悉Heroku,但我可以回答你的基本问题。 gunicorn是一个用于运行Python web的HTTP服务器应用程序通过WSGI。 web.py是一个使用WSGI创建Python Web应用程序的框架。 所以你并不需要一起使用这两者的教程,因为你需要做的就是弄清楚如何将web.py应用程序的WSGI入口点传递给gunicorn。 WSGI应用程序只是一个可调用的Python,它具有正确的接口,即它需要某些参数并返回特定的响应。有关更多信息,请参阅此WSGI教程。 import web $ bweb应用程序中的hello world应用程序如下所示: $ b urls =('/','index') class index: def GET(self): returnHello,世界! $ b $ if if __name__ ==__main__: app = web.application(urls,globals()) app.run() 但是,这并没有公开WSXI应用程序需要哪些gunicorn。 web.py通过 wsgifunc 方法提供WSGI应用程序。 > web.application 。我们可以通过在索引类中添加以下内容将其添加到test.py: #用于使用任何wsgi服务器 wsgi_app = web.application(urls,globals())。wsgifunc() 在使用 Apache + mod_wsgi - 对于使用gunicorn的我们来说,Python代码是一样的,这不是巧合,因为这正是WSGI为您提供的 - 一种标准的编写Python的方式,以便它可以可以使用任何支持WSGI的服务器进行部署。 正如 gunicorn docs ,然后我们可以在 test 的 wsgi_app 成员处指出gunicorn (tmp)day @ office:〜/ tmp $ gunicorn test:wsgi_app 2012-12-03 23:31 :11 [19265] [INFO]启动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]使用worker:sync 2012-12-03 23:31:11 [19268] [INFO]使用pid启动worker:19268 My question is basically what's in the title: how can I setup gunicorn to run a web.py app? (Also, if there are any differences, how would I do it on 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). 解决方案 I'm afraid I'm not familar with Heroku, but I can answer your basic question.gunicorn is a HTTP server for running Python web apps via WSGI. web.py is a framework for creating Python web apps using WSGI.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. A WSGI application is just a Python callable with the right interface i.e. it takes certain parameters and returns a certain response. See this WSGI tutorial for more.The "hello world" application from the web.py tutorial looks like this test.py:import weburls = ( '/', '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.web.py provides a WSGI application via the wsgifunc method of web.application. We can add this to test.py by adding the following after the index class:# For serving using any wsgi serverwsgi_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.As explained in the gunicorn docs, we can then point gunicorn at the wsgi_app member of the test module as follows:(tmp)day@office:~/tmp$ gunicorn test:wsgi_app2012-12-03 23:31:11 [19265] [INFO] Starting gunicorn 0.16.12012-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: sync2012-12-03 23:31:11 [19268] [INFO] Booting worker with pid: 19268 这篇关于web.py和gunicorn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-22 20:21