[Twisted-web] [Twisted-Python] twisted.web questions

Christopher Armstrong radix at twistedmatrix.com
Mon Jun 14 19:28:25 EDT 2010


On Mon, Jun 14, 2010 at 5:42 PM, Allen Bierbaum <abierbaum at gmail.com> wrote:
> Something like this:
>
>   def _bg_GET(self, request):
>       # Do something slow
>       request.write(result)
>       return request
>    def render_GET(self, request):
>       d = deferToThread(self._bg_GET, request)
>       d.addCallback(lambda r: r.finish())
>       return NOT_DONE_YET
>
> That seems to work, but I would like to
>    a) not use the deferToThread() and
>    b) do something that doesn't sacrifice too much performance.
>
> At the end of the day, what I would really like is something in render_GET
> that checks the return from calling the internal GET method and if it is a
> deferred it adds a callback to finish the request, if not it simply returns
> the result.  (this would allow me to use deferToThread, inlineCallbacks, etc
> as needed in my get methods)
>
> Can you see any issues with doing something like this?  (it seems almost too
> simple so I expect that I must be missing something)

Yeah, don't call request methods (like request.write) from the
function that gets run in a thread. You should be calling
request.write in the callback, or with callFromThread in the threaded
function.

-- 
Christopher Armstrong
http://radix.twistedmatrix.com/
http://planet-if.com/



More information about the Twisted-web mailing list