[Twisted-Python] Sequential use of asynchronous calls
Maarten ter Huurne
maarten at treewalker.org
Sat May 26 20:48:51 MDT 2007
On Saturday 26 May 2007, Ben Artin wrote:
> > 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.
>
> If the argument to the decorator is not a generator (i.e., it's a
> regular function), then you should just treat is as a generator that
> immediately returned.
Since it is used as a decorator, the @sequential line will be written by the
same person who wrote the function itself. If the author of the function
knows it is not a generator, why would he apply @sequential (or
@inlineCallbacks) to it?
> For example,
>
> @sequential
> def foo():
> yield retval
>
> should just behave the same as
>
> @sequential
> def foo():
> return retval
That behaviour would be: return a Deferred on which:
- callback is called immediately if retval is not a Deferred
- errback is called immediately if the function raises an exception
- callback or errback is called when the retval Deferred fires
And the result passed to the callback would be None, not retval.
Applying maybeDeferred to the return value sounds more useful, since that will
do the Deferred part, but will also preserve the result.
By the way, without "@sequential" the closest non-generator equivalent of
def foo():
yield retval
would be
def foo():
return (retval, )
In other words, it would return a sequence containing retval rather than
retval itself.
> I can't think of any case where passing a non-generator into
> @sequential is intended to mean anything other than this.
But should it be given any interpretation at all? Unless there is a real use
case for it, I think it's better to consider it an error.
Bye,
Maarten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20070527/16a6b6d7/attachment.sig>
More information about the Twisted-Python
mailing list