Created
February 12, 2015 09:06
-
-
Save slauwers/477ddf9bbd4fe43a8978 to your computer and use it in GitHub Desktop.
The dangers of inlineCallbacks
This file contains 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
$ time python /tmp/test_cb.py | |
done | |
done | |
done | |
done | |
done | |
python /tmp/test.py 0.05s user 0.01s system 6% cpu 1.065 total | |
$ time python /tmp/test_inline.py | |
done | |
done | |
done | |
done | |
done | |
python /tmp/test.py 0.04s user 0.01s system 1% cpu 5.056 total |
This file contains 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
#!/usr/bin/env python | |
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
from twisted.internet import (reactor, defer) | |
def doSomething(): | |
d = defer.Deferred() | |
reactor.callLater(1, d.callback, None) | |
return d | |
def loop(): | |
for i in xrange(5): | |
d = doSomething().addCallback(lambda _: print("done")) | |
d.addCallback(lambda _: reactor.stop()) | |
def main(): | |
loop() | |
if __name__ == '__main__': | |
main() | |
reactor.run() |
This file contains 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
#!/usr/bin/env python | |
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
from twisted.internet import (reactor, defer) | |
def doSomething(): | |
d = defer.Deferred() | |
reactor.callLater(1, d.callback, None) | |
return d | |
@defer.inlineCallbacks | |
def loop(): | |
for i in xrange(5): | |
yield doSomething() | |
print("done") | |
reactor.stop() | |
def main(): | |
loop() | |
if __name__ == '__main__': | |
main() | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment