On Mon, Jun 14, 2010 at 6:28 PM, Christopher Armstrong < radix@twistedmatrix.com> wrote:
On Mon, Jun 14, 2010 at 5:42 PM, Allen Bierbaum <abierbaum@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.
Good tip. Looks like the wsgi wrapper has a nice little helper method to support this: http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/... <http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/...> Thanks, Allen