[Twisted-Python] Resource.render() returning NOT_DONE_YET

Glyph Lefkowitz glyph at twistedmatrix.com
Sat May 10 10:31:52 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Friday, May 9, 2003, at 10:22 PM, Clark C. Evans wrote:

> On Fri, May 09, 2003 at 05:02:30PM +0200, Matthias Urlichs wrote:
> | Clark C. Evans schrub am Mon, 28 Apr 2003 16:09:35 +0000:
> | I think that wouldn't work. Consider:

[some code which I basically repeat below]

> | This ends up calling req.finish() befor req.write().
>
> Just beacuse you can write code that will violate a
> given protocol doesn't mean it's a bad protocol.

While in theory he _could_, this example doesn't :-)  Chained Deferred 
results will make this Do The Right Thing.  To be sure, I asked the 
Python interpreter about it -

Python 2.2.2 (#1, 01/12/03, 07:51:34)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> from twisted.internet.defer import Deferred
 >>> def render(req):
...     d = somewhere.getDeferred()
...     d.addCallback(render_more, req)
...     return d
...
 >>> def render_more(result, req):
...     d = somewhere.getMoreDeferred(result)
...     d.addCallback(render_even_more, req)
...     return d
...
 >>> def render_even_more(result, req):
...     req.write("Done!")
...
 >>> class MyRequest:
...     def write(self, w):
...         self.events.append(('write', w))
...     def finish(self):
...         self.events.append('finish')
...
 >>> class Somewhere:
...     def getDeferred(self):
...         self.d1 = Deferred()
...         return self.d1
...     def getMoreDeferred(self, result):
...         self.d2 = Deferred()
...         return self.d2
...
 >>> somewhere = Somewhere()
 >>> def finishHim(lastResult, req):
...     req.finish()
...
 >>> def runIt():
...     r = MyRequest()
...     e = r.events = []
...     d = render(r)
...     d.addCallback(finishHim, r)
...     somewhere.d1.callback("result1")
...     somewhere.d2.callback("result2")
...     print e
...
 >>> runIt()
[('write', 'Done!'), 'finish']


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (Darwin)

iD8DBQE+vQ1evVGR4uSOE2wRAkTIAKCESzfXgpQ5Ri5oYbjAqzlEt+xB7gCbBW+p
0FObyUElLwWVroj9lusUMR4=
=hzmx
-----END PGP SIGNATURE-----





More information about the Twisted-Python mailing list