Created
June 16, 2016 13:18
-
-
Save lfdversluis/c1d3d310dc1e6f56c85ac7edb6afe9e6 to your computer and use it in GitHub Desktop.
Generators with @inlineCallbacks?
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 time import sleep | |
from twisted.internet import reactor | |
from twisted.internet.defer import inlineCallbacks | |
from twisted.internet.task import deferLater | |
@inlineCallbacks | |
def a(): | |
yield deferLater(reactor, 1, lambda: None) # Now the blocking function is made async, we need to yield it. | |
for i in xrange(10): | |
yield i | |
# note NO inlinecallbacks | |
def a1(): | |
sleep(1) # suppose this is a blocking function | |
for i in xrange(10): | |
yield i | |
@inlineCallbacks | |
def b(): | |
d = yield a1() | |
print d | |
b = d.next() | |
print b | |
d = yield a() # We yield the deferred from the @inlineCallbacks | |
print d # None - I expect this to be a generator object or the int itself | |
b = d.next() # Crashes? Why doesn't this give me 0 ? | |
print b | |
@inlineCallbacks | |
def c(): | |
yield b() | |
reactor.stop() | |
reactor.callWhenRunning(c) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment