[Twisted-Python] Newbie Q: The first example in Fettig's book confused me
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(): printTime() print "Stopping reactor" reactor.stop()
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
On Sun, 4 Jun 2006 09:32:32 +0800, Mike Meng <meng.yan@gmail.com> wrote:
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?
Presumably it took you 5 seconds to type all that code interactively, but each callLater is basing the time it wants to run off of the time that it was when you completed each line :). Either learn to type really, really fast, or put all the callLater calls into a function which you then call interactively.
Hi. I am trying to decide where to put message sizing for a custom protocol. I have set up the connections to be as generic as possible so message handling in as much as possible is being done by the connection handler that is aware of the connection it is handling at the time. There are a few things I need to keep track of at the connection (protocol) level such as original handshake and message id's that I am tracking per connection, but for the most part, everything else can be handled pretty generically by the connection handler. In any case, where to deal with message sizing? My thought was that the connection handler should deal with it since it should also be responsible for encode and decode of messages. So to process or prepare a message size seems like it fits in the connection handler also. I realize Banana, for example does sizing in the protocol itself but it does not anticipate handing the message off to a handler for such things. Anyway, it would be great if someone could confirm my reasoning or has some comments on this. Many thanks, Regards, David
I guess I answered myself. Subclassing my base protocol to handle the marshalling between sending and receiving appears more transparent. Regards, David David Pratt wrote:
Hi. I am trying to decide where to put message sizing for a custom protocol. I have set up the connections to be as generic as possible so message handling in as much as possible is being done by the connection handler that is aware of the connection it is handling at the time. There are a few things I need to keep track of at the connection (protocol) level such as original handshake and message id's that I am tracking per connection, but for the most part, everything else can be handled pretty generically by the connection handler.
In any case, where to deal with message sizing? My thought was that the connection handler should deal with it since it should also be responsible for encode and decode of messages. So to process or prepare a message size seems like it fits in the connection handler also.
I realize Banana, for example does sizing in the protocol itself but it does not anticipate handing the message off to a handler for such things. Anyway, it would be great if someone could confirm my reasoning or has some comments on this. Many thanks,
Regards, David
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
David Pratt
-
glyph@divmod.com
-
Mike Meng