Skip to content

Instantly share code, notes, and snippets.

@meejah
Created October 20, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meejah/d6f9fba8c080c35cba6f to your computer and use it in GitHub Desktop.
Save meejah/d6f9fba8c080c35cba6f to your computer and use it in GitHub Desktop.
twisted2.py
from twisted.internet.defer import Deferred, inlineCallbacks, returnValue
d0 = Deferred()
d1 = Deferred()
d2 = Deferred()
def check_value(arg, gold):
print "check_value", arg, gold
assert gold == arg
return arg
@inlineCallbacks
def foo(arg):
print 'foo', arg
x = yield d1
x = 'foo-' + str(x)
print 'foo() is returning', x
returnValue(x)
def quux(arg):
print "quux", arg
d = foo('blam')
print 'quux() is returning', d
return d
@inlineCallbacks
def bar(arg):
print 'bar', arg
z = yield quux(arg)
print 'bar() is returning', z
returnValue(z)
d2.callback('d2')
d1.callback('d1')
d0.addCallback(bar)
d2.addCallback(check_value, 'd2')
d1.addCallback(check_value, 'd1')
d0.callback('d0')
d2.addCallback(check_value, 'd2')
d0.addCallback(check_value, 'foo-d1')
# somehow, d1's result has become None here ...
d1.addCallback(check_value, 'd1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment