[Python-ideas] async: feedback on EventLoop API
Guido van Rossum
guido at python.org
Fri Dec 21 17:03:21 CET 2012
On Fri, Dec 21, 2012 at 7:57 AM, Jasper St. Pierre
<jstpierre at mecheye.net> wrote:
> Aha, that cleared it up, thanks. wait_one_async() takes an iterable of
> tasks, and returns a Future that will fire when a Future completes,
> containing that Future.
>
> I can't think of anything *wrong* with that, except that if anything, 1) it
> feels like a bit of an abuse to use Futures this way, 2) it feels a bit
> low-level.
But not more low-level than callbacks. Once you're used to coroutines
and Futures, you don't want things that use callbacks. Fortunately
there's an easy way to turn a callback into a Future:
f = Future()
old_style_async(callback=f.set_result)
result = yield from f
Assuming old_style_async() calls its callback with one arg, a useful
result, that result will now end up in the variable 'result'. If this
happens a lot it's easy to wrap it in a helper function, so you can
write:
result = yield from wrap_in_future(old_style_async)
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list