2

Pyramid uses it's own Waitress web server for development purposes, but I want to serve my WSGI app under Tornado. I think I should configure it using the pserve .ini files, but I can't get it to work

tshepang
  • 12,111
  • 21
  • 91
  • 136
stipetic
  • 41
  • 1
  • 7
  • 1
    Not really what you asked for, so making it a comment, not an answer. While Tornado CAN serve WSGI, it's not really recommended, since WSGI doesn't allow the asynchronous processing which is the real reason for running Tornado. IMHO flup is a much more lean and simple way to deploy WSGI-apps, and serves as a gateway between a WSGI-app and AJP/FCGI/SCGI/CGI. You should go for the fork-versions in production, since it get's around the [Python GIL](http://wiki.python.org/moin/GlobalInterpreterLock). – Rawler Mar 30 '12 at 12:17

2 Answers2

6

The Pyramid application can be loaded from the INI files easily. From there you just pass the wsgi app into Tornado's WSGIContainer.

from pyramid.paster import get_app

app = get_app('development.ini')
container = tornado.wsgi.WSGIContainer(app)
Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
  • Is there a way to start the app via `pserve` instead of a runner script? – zakdances Jun 05 '13 at 05:42
  • 1
    Well the pserve server runners are entirely pluggable via PasteDeploy, so feel free to write your own (it's quite simple if you figure out setuptools entry points). I am not familiar with a pre-baked runner for tornado. – Michael Merickel Jun 05 '13 at 06:50
4

Again, not really recommending running WSGI under Tornado, since it gives you none of the advantages of Tornado.

Should you still want to do it for some reason, the second example of the docs seems to be what you are looking for: http://www.tornadoweb.org/documentation/wsgi.html

def simple_app(environ, start_response):
    status = "200 OK"
    response_headers = [("Content-type", "text/plain")]
    start_response(status, response_headers)
    return ["Hello world!\n"]

container = tornado.wsgi.WSGIContainer(simple_app)
http_server = tornado.httpserver.HTTPServer(container)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Rawler
  • 1,480
  • 1
  • 11
  • 23
  • Okay, which server should I use then? Preferably one that's very fast – stipetic Mar 30 '12 at 12:23
  • Depends on your needs. If speed is everything, you should _probably_ go with Tornado, but not deploy as a WSGI-app, but a native asynchronous Tornado-app. (Especially if long-polling is a concern) If your existing WSGI-interface app is a must, http://nichol.as/benchmark-of-python-web-servers might be helpful. Depending on your app, a good strategy can be to use a slightly slower, but simple and lean server, combined with a well-tuned Varnish/nginx caching frontend. – Rawler Mar 30 '12 at 13:10
  • For example, if much of the app is generating a few highly-visited common index-views, a well-tuned nginx could make sure just one request every few seconds hit the Python-code, and any application-server can handle that. – Rawler Mar 30 '12 at 13:14
  • Personally I find tornado to be a much more pleasant web server to use over pyramid, so OP's question makes sense to me; I'd rather develop on tornado + waitress and then slowly migrate over to a proper async model when time and opportunity allow. Thank you for posting this solution, even if it doesn't have an obvious use case. – NuclearPeon Sep 15 '15 at 22:10