[Twisted-web] Twisted and WSGI...

glyph at divmod.com glyph at divmod.com
Wed Apr 5 19:07:55 CDT 2006

On Wed, 05 Apr 2006 14:39:36 -0500, Ian Bicking <ianb at colorstudy.com> wrote:

>I think "disdain" is too strong.  There's a lack of any complete proposal 
>which would work, especially one that doesn't have Deferred or other 
>particular bits of code as a prerequesite.  There was discussion a long time 
>ago, but it drifted off.  More generally, there's no larger consensus on how 
>to do asynchronous programming in Python, though there's obvious consensus 
>on how to do synchronous programming (that is: with functions).

There is plenty of consensus on how to do asynchronous programming with Python: use Twisted ;-).

>Which reminds me -- I started doing this in Paste, but got bogged down in 
>all the setup and imports I didn't understand, and then subscribed here and 
>never followed up.  Can someone provide an example of a simple function that 
>would look like:

Taking this approach restricts your deployment to be web-only, which eliminates a substantial advantage of Twisted.  It isn't hard to implement, though:

from twisted.web2 import server, wsgi
from twisted.web2.channel import http
from twisted.internet import reactor

>def serve_with_twisted(hosts, wsgi_app, **kw):
>     """Serve wsgi_app indefinitely
>     hosts is a list of 'address:port', and wsgi_app is a
>     WSGI application.  **kw is... whatever other interesting things
>     you might want to use to configure a Twisted server
>     """
      factory = http.HTTPFactory(server.Site(wsgi.WSGIResource(app)))
      for hostportpair in hosts:
           host, portstr = hostportpair.split(':')
           port = int(portstr)
           reactor.listenTCP(port, factory, interface=host)

This is completely untested, but it should at least get you going in the right direction.

More information about the Twisted-web mailing list