Re: [Twisted-Python] twisted.web questions

Allen Bierbaum <[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
On Mon, Jun 14, 2010 at 6:28 PM, Christopher Armstrong <
[email protected]> wrote:

> On Mon, Jun 14, 2010 at 5:42 PM, Allen Bierbaum <[email protected]>
> 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/web/wsgi.py#L257

<http://twistedmatrix.com/trac/browser/tags/releases/twisted-10.0.0//twisted/web/wsgi.py#L257>
Thanks,
Allen

_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.