Last active
May 23, 2016 17:13
-
-
Save nakamuray/c5ae5123d6dd67327e2bdf309357e7da to your computer and use it in GitHub Desktop.
Deferred and Future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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