[Twisted-Python] client connecting to 2 servers (nonsimultaneously)

Jason Rennie jrennie at gmail.com
Wed Jan 12 08:15:59 MST 2011


Habit, mostly.  I think it's a good habit, though.  The reason is that I
wanted to make sure the __init__ code completed before the connectTCP was
called.  In this case, it doesn't matter, but if I had a number of things I
wanted to do in __init__, it might matter.  Consider:

class MyClientProtocol(Protocol):
      def connectionMade(self):
        self.factory.numConnections += 1

class MyClientFactory(Factory):
      def __init__(self):
        reactor.connectTCP(host, port, self)
  self.numConnections = 0

'course, I'm sure you'd put reactor.connectTCP *after* the numConnections
initializer, but using callWhenRunning ensures that the order doesn't
matter, so it's one less thing you have to worry about :-)

Jason

On Wed, Jan 12, 2011 at 4:49 AM, <benjamin.bertrand at lfv.se> wrote:

> One small question about the following code:
> Why did you use reactor.callWhenRunning in the __init__ method?
> Why not calling directly reactor.connectTCP?
>
> Cheers,
>
> Benjamin
>
>
-- 
Jason Rennie
Research Scientist, ITA Software
617-714-2645
http://www.itasoftware.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20110112/74ab6e3d/attachment-0001.html>


More information about the Twisted-Python mailing list