[Python-ideas] Async API

Guido van Rossum guido at python.org
Fri Oct 26 18:36:59 CEST 2012

On Fri, Oct 26, 2012 at 8:52 AM, Laurens Van Houtven <_ at lvh.cc> wrote:
> err, I suppose the missing bit there is that you'll probably want to:
> reactor.callLater(timeout, d.cancel)
> As opposed to calling d.cancel() directly. (That snippet was in
> bpython-urwid with the reactor running in the background, but I doubt it'd
> work well anywhere else outside of manholes :))

So I think that Yuri's original problem statement, transformed to
Twisted+Deferred, might still apply, depending on how you implement
it. Yuri essentially did this:

def foobar():  # a task
        yield <blocking action>
        # must clean up regardless of whether action succeeded or failed:
        yield <blocking cleanup>

He then calls this with a timeout, with the semantics that if the
generator is blocked in a yield when the timeout arrives, that yield
raises a Timeout exception (and at no other time is Timeout raised).
The problem with this is that if the action succeeds within the
timeout, but barely, there's a chance that the cleanup of a
*successful* action receives the Timeout exception. Apparently this
bit Yuri. I'm not sure how you'd model that using just Deferreds, but
using inlineCallbacks it seems the same thing might happen. Using
Deferreds, I assume there's a common pattern to implement this that
doesn't have this problem. Of course, using coroutines, there is too
-- spawn the cleanup as an independent task.

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list