On Mon, Jun 14, 2010 at 6:28 PM, Christopher Armstrong <span dir="ltr">&lt;<a href="mailto:radix@twistedmatrix.com">radix@twistedmatrix.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Mon, Jun 14, 2010 at 5:42 PM, Allen Bierbaum &lt;<a href="mailto:abierbaum@gmail.com">abierbaum@gmail.com</a>&gt; wrote:<br>
&gt; Something like this:<br>
&gt;<br>
&gt;   def _bg_GET(self, request):<br>
&gt;       # Do something slow<br>
&gt;       request.write(result)<br>
&gt;       return request<br>
&gt;    def render_GET(self, request):<br>
&gt;       d = deferToThread(self._bg_GET, request)<br>
&gt;       d.addCallback(lambda r: r.finish())<br>
&gt;       return NOT_DONE_YET<br>
&gt;<br>
&gt; That seems to work, but I would like to<br>
&gt;    a) not use the deferToThread() and<br>
&gt;    b) do something that doesn&#39;t sacrifice too much performance.<br>
&gt;<br>
&gt; At the end of the day, what I would really like is something in render_GET<br>
&gt; that checks the return from calling the internal GET method and if it is a<br>
&gt; deferred it adds a callback to finish the request, if not it simply returns<br>
&gt; the result.  (this would allow me to use deferToThread, inlineCallbacks, etc<br>
&gt; as needed in my get methods)<br>
&gt;<br>
&gt; Can you see any issues with doing something like this?  (it seems almost too<br>
&gt; simple so I expect that I must be missing something)<br>
<br>
</div>Yeah, don&#39;t call request methods (like request.write) from the<br>
function that gets run in a thread. You should be calling<br>
request.write in the callback, or with callFromThread in the threaded<br>
function.<br></blockquote><div><br></div><div>Good tip.  Looks like the wsgi wrapper has a nice little helper method to support this:</div><div><br></div><div><a href="http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/web/wsgi.py#L257">http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/web/wsgi.py#L257</a></div>
<div><br></div><div><a href="http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/web/wsgi.py#L257"></a>Thanks,</div><div>Allen</div><div><br></div></div>