Skip to content

Instantly share code, notes, and snippets.

@lfdversluis
Created June 16, 2016 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfdversluis/c1d3d310dc1e6f56c85ac7edb6afe9e6 to your computer and use it in GitHub Desktop.
Save lfdversluis/c1d3d310dc1e6f56c85ac7edb6afe9e6 to your computer and use it in GitHub Desktop.
Generators with @inlineCallbacks?
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