[Twisted-Python] client connecting to 2 servers (nonsimultaneously)
exarkun at twistedmatrix.com
exarkun at twistedmatrix.com
Wed Jan 12 08:26:37 MST 2011
On 03:15 pm, jrennie at gmail.com wrote:
>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 :-)
Although note that if the reactor is already running when you
instantiate the factory, then callWhenRunning will immediately call the
function you pass to it, as demonstrated by this transcript in which the
reactor is already running:
>>> def foo():
... print 'foo running'
... >>> from twisted.internet import reactor
>>> reactor.callWhenRunning(foo)
foo running
>>>
Jean-Paul
More information about the Twisted-Python
mailing list