Re: [Twisted-web] [Twisted-Python] twisted.web questions
[..]
There's a branch of Twisted that implements this, hopefully it will
eventually be merged: http://twistedmatrix.com/trac/ticket/3711
Is there anything that is currently holding this up? (ie. is the design on this branch what will end up in the system or could it change dramatically?)
There are a bunch of review comments in the ticket. You could just write your own until this is ready, I've done it a number of times and it's a pretty small piece of code.
I started writing one of my own today (basically a wrapper in render_GET that calls an deferrable render_GET). 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) -Allen
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. -- Christopher Armstrong http://radix.twistedmatrix.com/ http://planet-if.com/
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/web/wsgi.py#L257> Thanks, Allen
participants (2)
-
Allen Bierbaum
-
Christopher Armstrong