[Python-ideas] Async API

Jasper St. Pierre jstpierre at mecheye.net
Fri Oct 26 19:14:56 CEST 2012


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.

> Thanks,
> Yury
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-- 
  Jasper



More information about the Python-ideas mailing list