Skip to content

Instantly share code, notes, and snippets.

@nakamuray
Last active May 23, 2016 17:13
Show Gist options
  • Save nakamuray/c5ae5123d6dd67327e2bdf309357e7da to your computer and use it in GitHub Desktop.
Save nakamuray/c5ae5123d6dd67327e2bdf309357e7da to your computer and use it in GitHub Desktop.
Deferred and Future
from twisted.internet import defer
from asyncio import futures, get_event_loop
d = defer.Deferred()
d.addCallback(lambda x: x + 1)
d.addCallback(lambda x: x * 2)
f1 = futures.Future()
f2 = futures.Future()
f1.add_done_callback(lambda f: f2.set_result(f.result() + 1))
f3 = futures.Future()
f2.add_done_callback(lambda f: f3.set_result(f.result() * 2))
d.callback(20)
f1.set_result(20)
get_event_loop().run_until_complete(f3)
assert d.result == f3.result() == 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment