This isn't a good use for Deferreds. Instead, try using reactor.callLater
:
from twisted.internet import reactor
def loopPrinting():
print chr(random.randint(97, 122))
reactor.callLater(1.0, loopPrinting)
loopPrinting()
reactor.run()
Or twisted.internet.task.LoopingCall
:
from twisted.internet import task, reactor
def loopPrinting():
print chr(random.randint(97, 122))
loop = task.LoopingCall(loopPrinting)
loop.start(1.0)
reactor.run()
Your Deferred-based version has a couple problems. First, it defines a callback on a Deferred that returns the same Deferred. Returning a Deferred (let's call it a
) from a callback on another Deferred (let's call it b
) does something called "chaining". It makes b
pause its callback chain until a
has a result. In the case where a
and b
are actually the same Deferred instance, this makes little or no sense.
Second, when adding a callback to a Deferred that already has a result, the callback will be called immediately. In your case, your callback adds another callback. And that callback adds another callback. So you have an infinite loop all contained inside your d.addCallback(loopPrinting)
line. This will prevent the reactor from ever running, breaking any other part of your program.