<div dir="ltr"><div><div>Hi Luca,<br><br></div>Unfortunately I haven't thought yet about the interactions between WSGI and Tulip or PEP 3156. While I am pretty familiar with WSGI, I have never used its async features, so I can't be much of a help. My best guess is that we won't make any changes to WSGI to support PEP 3156 in Python 3.4, but that once that is out, some folks will come up with an improved design for WSGI that supports interoperability with standard async event loops. OTOH, maybe you can read up on the PEP and check out the Tulip implementation (<a href="http://code.google.com/p/tulip/">http://code.google.com/p/tulip/</a>) and maybe you can come up with a suitable design for integrating PEP 3156 into WSGI? Though it may have to be named WSGI 2.0 to emphasize that it is backwards incompatible.<br>

<br></div>--Guido<br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Mar 24, 2013 at 2:18 PM, Luca Sbardella <span dir="ltr"><<a href="mailto:luca.sbardella@gmail.com" target="_blank">luca.sbardella@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>first time here, I'm Luca and I write lots of python of the asynchronous variety.</div>

<div>This question is about wsgi and the way pulsar <a href="http://quantmind.github.com/pulsar/" target="_blank">http://quantmind.github.com/pulsar/</a> handles asynchronous wsgi responses.</div>
<div><br></div><div>Yesterday I sent a message to the python-dev mailing list regarding wsgiref.validator, this is the original message</div><div><br></div><div><div class="im"><div><font color="#000000" face="Arial, Verdana, Geneva, Bitstream Vera Sans, Helvetica, sans-serif"><span style="font-size:15px;line-height:21px">I have an asynchronous wsgi application handler which yields empty bytes before it is ready to yield the response body and, importantly, to call start_response.</span></font></div>


<div><font color="#000000" face="Arial, Verdana, Geneva, Bitstream Vera Sans, Helvetica, sans-serif"><span style="font-size:15px;line-height:21px"><br></span></font></div><div><font color="#000000" face="Arial, Verdana, Geneva, Bitstream Vera Sans, Helvetica, sans-serif"><span style="font-size:15px;line-height:21px">Something like this:</span></font></div>


</div><div><div class="im"><font color="#000000" face="Arial, Verdana, Geneva, Bitstream Vera Sans, Helvetica, sans-serif"><span style="font-size:15px;line-height:21px"><div><br></div><div>def wsgi_handler(environ, start_response):</div>

<div>        body = generate_body(environ)</div>
</span></font></div><font color="#000000" face="Arial, Verdana, Geneva, Bitstream Vera Sans, Helvetica, sans-serif"><div class="im"><div style="font-size:15px;line-height:21px">        body = maybe_async(body)</div><div style="font-size:15px;line-height:21px">


        while is_async(body):</div><div style="font-size:15px;line-height:21px">            yield b''</div><div style="font-size:15px;line-height:21px">        start_response(...)</div><div style="font-size:15px;line-height:21px">


        ...</div><div style="font-size:15px;line-height:21px"><br></div><div style="font-size:15px;line-height:21px">I started using wsgiref.validator recently, nice little gem in the standard lib, and I discovered that the above handler does not validate! Disaster.</div>


<div style="font-size:15px;line-height:21px">Reading pep 3333</div><div style="font-size:15px;line-height:21px"> </div><div style="font-size:15px;line-height:21px">"the application <strong>must</strong> invoke the <tt style="line-height:normal">start_response()</tt> callable before the iterable yields its first body bytestring, so that the server can send the headers before any body content. However, this invocation <strong>may</strong> be performed by the iterable's first iteration, so servers <strong>must not</strong> assume that <tt style="line-height:normal">start_response()</tt> has been called before they begin iterating over the iterable."</div>


<div style="font-size:15px;line-height:21px"><br></div></div><div class="im"><div style="font-size:15px;line-height:21px">The pseudocode above does yields bytes before start_response, but they are not *body* bytes, they are empty bytes so that the asynchronous wsgi server releases the eventloop and call back at the next eventloop iteration.</div>


<div><br></div></div></font></div></div><div><br></div><div>And the response was</div><div class="im"><div class="gmail_extra"><br><br><div class="gmail_quote"><span dir="ltr"></span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">



<div><div>>PJ Eby wrote:<br>
>> The validator is correct for the spec.  You *must* call<br>
>> start_response() before yielding any strings at all.<br>
><br>
><br>
> Thanks for response PJ,<br>
> that is what I, unfortunately, didn't want to hear, the validator being<br>
> correct for the "spec" means I can't use it for my asynchronous stuff, which<br>
> is a shame :-(((<br>
> But why commit to send headers when you may not know about your response?<br>
> Sorry if this is the wrong mailing list for the issue, I'll adjust as I go<br>
> along.<br>
<br>
</div></div>Because async was added as an afterthought to WSGI about nine years<br>
ago, and we didn't get it right, but it long ago was too late to do<br>
anything about it.  A properly async WSGI implementation will probably<br>
have to wait for Tulip (Guido's project to bring a standard async<br>
programming API to Python).<br>
</blockquote></div><br></div></div><div class="gmail_extra">and so here I am.</div><div class="gmail_extra">I know tulip is on its early stages but is there anything on the pipeline about wsgi?</div><div class="gmail_extra">


Happy to help if needed.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Regards</div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra">Luca</div><div class="gmail_extra"><br></div></font></span></div>


</blockquote></div><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)
</div>