[Web-SIG] Async API - example of my implementation

Phillip J. Eby pje at telecommunity.com
Sat Oct 16 07:47:19 CEST 2004


At 10:16 PM 10/15/04 -0400, Peter Hunt wrote:
>I'm working on getting subversion running again, but for now, take a
>look at how I write my Twisted WSGI async apps.
>
>def blocking_call():
>     d = defer.Deferred()
>     reactor.callLater(2, d.callback, None)
>     return d
>
>def phase2(result, environ):
>     environ["thetime"] = time.time()
>     environ["twisted.wsgi.resume"]()
>
>def blocking_async_app(environ, start_response):
>     write = start_response("200 OK", [("Content-type","text/plain")])
>     yield "the time right now is " + `time.time()` + "\n"
>     blocking_call().addCallback(phase2, environ)
>     yield ""
>     yield "the time now is " + `environ["thetime"]`
>
>Is this acceptible?
>
>Basically, when in the special async mode, the gateway iterates over
>the application iterator until it hits a "". It then lets the app do
>its thing until environ["twisted.wsgi.resume"]() is called, at which
>point it repeats this process until StopIteration.
>
>What do you think?

I think an explicit pause operation is better, e.g.:

     def blocking_async_app(environ,start_response):
         start_response("200 OK", [("Content-type","text/plain")])
         yield "doing something"

         resume = environ['wsgi.pause_iteration']()
         def phase2(result):
             environ["thetime"] = time.time()
             resume()

         blocking_call().addCallback(phase2)
         yield ""

         # Won't get here till 'resume' is called
         yield "the time now is " + `environ["thetime"]`


This is basically the first of the two alternative API proposals that's 
currently outstanding.  One issue that is not addressed either in your 
example or in the previous proposal is error handling/timeouts.  Suppose 
resume() is never called?   How do we define what "never" is?  This is just 
one open issue with the current async API proposals.



More information about the Web-SIG mailing list