[Twisted-web] Twisted and WSGI...

Ed Suominen general at eepatents.com
Wed Apr 5 14:01:39 CDT 2006


m h wrote:
> I'm new to this list.  (Am beginning to think more about learning
> twisted... for the third time ;) ).  I've got a question for you all
> and hope that you don't take it the wrong way.  It's not meant to be
> flame bait.

Don't worry, I don't see how it could remotely be considered anything
but an honest and constructive inquiry!

> As WSGI is going into python 2.5, I was wondering what the general
> consensus is among twisted web devs about it?  Does the WSGI model map
> well into twisted's asynchronous model?  Does twisted already have
> everything it needs, so WSGI doesn't bring much to the table?

For a Twisted HTTP server (web2) to serve as a WSGI gateway and run a
WSGI application, the application has to either operate under the
Twisted non-blocking model or has to be run in a thread. The latter is
what's being done with everything run under twisted.web2.wsgi right now;
the WSGI Resource object returns the result of a deferToThread(f) where
f is the potentially blocking WSGI app.

However, you could write your WSGI app in a way that quickly returns a
deferred to its output and then crank away at its leisure with some
Twisted-friendly technique like iterations of reactor.callLater(). (The
Twisted devs may correct me and suggest more Twisted-friendly techniques
for "cranking away" at something...) The advantage of the async approach
would be (1) it's more in tune with overall Twisted approach, and (2)
you could have partial results start to show up on the page before the
WSGI app is completely done producing them.

> It appears that as AJAX becomes more popular then the asynchronous
> model might scale better with many small requests.  Say I have a WSGI
> app and I want to port it to twisted.  How easy is that?  (I know
> there is a twisted wsgi module)  Do I just need to wrap some logic in
> deferreds?

Since twisted.web2.wsgi currently runs the WSGI app in a thread (and
will no doubt keep the option of doing so even if it broadens to allow
for async WSGI apps), it's very simple. You just let your app block all
it wants, and the deferred that results from deferToThread will fire
when it's done.

> Are any twisted people using WSGI?

Yes, I am. I use it to run Trac over Twisted.web2 at my
http://foss.eepatents.com site. You can see both an example of it
working and some pertinent code at
http://foss.eepatents.com/DynamicSite/. That is, by the way, one of
several virtual-host sites all running from a single instance of
DynamicSite, my same twisted.web2 HTTP server.

Good luck!

-Ed Suominen




More information about the Twisted-web mailing list