[Python-ideas] Async API

Yury Selivanov yselivanov.ml at gmail.com
Fri Oct 26 19:49:58 CEST 2012


On 2012-10-26, at 1:14 PM, Jasper St. Pierre <jstpierre at mecheye.net> wrote:
> On Fri, Oct 26, 2012 at 1:06 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> 
> ... snip ...
> 
>> Let me ask you a question that may help me and others to understand
>> how inlineCallbacks works.
>> 
>> If you write the following:
>> 
>>   def func():
>>       try:
>>           yield one_thing()
>>           yield and_another()
>>       finally:
>>           yield and_finally()
>> 
>> Then each of those yields will create a separate Deferred object, that
>> 'inlineCallbacks' transparently dispatches via generator send/throw,
>> right?
> 
> one_thing() and and_another() and and_finally() should return
> Deferreds. inlineCallbacks gets those Deferreds, adds callbacks for
> completion/error, and resumes the generator at the appropriate time.
> You don't use the results from either Deferreds, so the values will
> just be thrown out. The yield/trampoline doesn't create any Deferreds
> for those operations itself.
> 
>> And if you 'yield func()' the same will happen--'inlineCallbacks' will
>> return a Deferred, that will have a result of 'func' execution?
> 
> You didn't decorate func with inlineCallbacks, but if you do, func()
> will give you Deferred. Note that func itself doesn't return any
> value. In Twisted land, this is done by defer.returnValue(), which
> uses exceptions to return a value to the trampoline. This maps well to
> the new sugar in 3.3.

Right, I forgot to decorate the 'func' with 'inlineCallbacks'.

If it is decorated, though, how can I invoke it with a timeout?

-
Yury


More information about the Python-ideas mailing list