[Twisted-Python] Newbie Q: The first example in Fettig's book confused me

Mike Meng meng.yan at gmail.com
Sat Jun 3 21:32:32 EDT 2006

Hi all,
    I'm a new Python programmer and just picked up Abe Fettig's "Twisted"
book. I tried out the first example, which use reactor.run() to drive
printTime and stopReactor callback functions to print time, and found some
strange things.

   I wrote the code in a .py file and run it, it's fine. However, when I
tried to write the code in interactive mode and run it from IDLE shell
window, all the four printTime were called at once. Why?

   Thank you in advance.

   The session is listed here:

>>> from twisted.internet import reactor
>>> import time
>>> def printTime():
    print "Current time is ", time.strftime("%H:%M:%S")

>>> def stopReactor():
        print "Stopping reactor"

>>> reactor.callLater(1, printTime)
<twisted.internet.base.DelayedCall instance at 0x00D9E288>
>>> reactor.callLater(2, printTime)
<twisted.internet.base.DelayedCall instance at 0x00E42210>
>>> reactor.callLater(3, printTime)
<twisted.internet.base.DelayedCall instance at 0x00DF8BC0>
>>> reactor.callLater(4, printTime)
<twisted.internet.base.DelayedCall instance at 0x00C74760>
>>> reactor.callLater(5, stopReactor)
<twisted.internet.base.DelayedCall instance at 0x0132E738>
>>> reactor.run()
Current time is  09:06:52
Current time is  09:06:52
Current time is  09:06:52
Current time is  09:06:52
Current time is  09:06:52
Stopping reactor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20060604/7d46ca45/attachment.htm 

More information about the Twisted-Python mailing list