[Twisted-Python] Sequential use of asynchronous calls
Maarten ter Huurne
maarten at treewalker.org
Sat May 26 15:10:08 EDT 2007
Hi,
Reading through the implementation of inlineCallbacks, I found some more
differences, but they are small.
In my implementation the decorator has a check whether the thing it is
decorating is really a generator. Without this check, you will get an
exception only when you start using the decorated function. I think it is
usually better to detect errors at an early stage. However, the check uses
compiler.consts.CO_GENERATOR, which is not documented in the Python Library
Reference, so using it might be risky.
My implementation only catches StandardError being raised by the generator.
That is a bad idea: it should certainly catch custom Exceptions as well. It
should probably catch everything, like inlineCallbacks does, so the
asynchronous equivalents of "finally" have a chance to clean things up on
system exit.
inlineCallbacks has protection against a Deferred being returned which has
already fired. My implementation treats that case the same as a Deferred that
will be handled by the reactor and will therefore recurse.
The way the "throw" call is done is slightly different:
http://twistedmatrix.com/trac/browser/trunk/twisted/internet/defer.py#L724
result = g.throw(result.type, result.value, result.tb)
My implementation:
traceback = result.getTracebackObject() \
if hasattr(result, 'getTracebackObject') else None
d = self.gen.throw(result.type, result.getErrorMessage(), traceback)
The "value" part is different: result.getErrorMessage() returns the error
message string, while result.value contains the exception object,
however "throw" is designed to act like "raise" and will accept both uses.
Also the "traceback" part is different: if the "tb" field is None,
getTracebackObject will construct a traceback object if possible. I don't
know if that extra feature will ever be useful in this particular use case.
To summarize:
InlineCallbacks seems to be better than my implementation, except perhaps:
- it might be useful to add a check whether the function to be decorated is
really a generator
- result.getTracebackObject() may or may not be better than result.tb
Bye,
Maarten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20070526/5c242b0f/attachment.pgp
More information about the Twisted-Python
mailing list