Skip to content

Commit

Permalink
Slight optimization in Coroutine.cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Allik committed Aug 27, 2012
1 parent 2bca21c commit b2af0fd
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions txcoroutine/__init__.py
Expand Up @@ -147,6 +147,9 @@ def unwindGenerator(*args, **kwargs):
return mergeFunctionMetadata(f, unwindGenerator)


_swallow_cancelled_error = lambda f: f.trap(CancelledError)


class Coroutine(Deferred):
# this is something like chaining, but firing of the other deferred does not cause this deferred to fire.
# also, we manually unchain and rechain as the coroutine yields new Deferreds.
Expand All @@ -170,15 +173,14 @@ def cancel(self):
# its clean-up routine, the inner Deferred hadn't yet actually been cancelled.
self.cancelling = True

# this errback is added as the last one, so anybody else who is already listening for CancelledError
# will still get it.
swallow_cancelled_error = lambda f: f.trap(CancelledError)
# the _swallow_cancelled_error errback is added as the last one, so anybody else who is already listening for
# CancelledError will still get it.

if self.depends_on:
self.depends_on.addErrback(swallow_cancelled_error)
self.depends_on.addErrback(_swallow_cancelled_error)
self.depends_on.cancel()

self.addErrback(swallow_cancelled_error)
self.addErrback(_swallow_cancelled_error)
Deferred.cancel(self)


Expand Down

0 comments on commit b2af0fd

Please sign in to comment.