Re: [Twisted-Python] Twisted-Python Digest, Vol 82, Issue 12
Sent from my LG phone twisted-python-request@twistedmatrix.com wrote:
Send Twisted-Python mailing list submissions to twisted-python@twistedmatrix.com
To subscribe or unsubscribe via the World Wide Web, visit http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python or, via email, send a message with subject or body 'help' to twisted-python-request@twistedmatrix.com
You can reach the person managing the list at twisted-python-owner@twistedmatrix.com
When replying, please edit your Subject line so it is more specific than "Re: Contents of Twisted-Python digest..."
Today's Topics:
1. Re: client connecting to 2 servers (nonsimultaneously) (benjamin.bertrand@lfv.se) 2. Re: client connecting to 2 servers (nonsimultaneously) (Jason Rennie) 3. Re: client connecting to 2 servers (nonsimultaneously) (exarkun@twistedmatrix.com) 4. Re: client connecting to 2 servers (nonsimultaneously) (Jason Rennie) 5. Re: client connecting to 2 servers (nonsimultaneously) (Glyph Lefkowitz)
----------------------------------------------------------------------
Message: 1 Date: Wed, 12 Jan 2011 09:49:34 +0000 From: <benjamin.bertrand@lfv.se> Subject: Re: [Twisted-Python] client connecting to 2 servers (nonsimultaneously) To: <twisted-python@twistedmatrix.com> Message-ID: <2C9A58914594E34AA28179DB776FABDF92E4@xw-exch04.lfv.se> Content-Type: text/plain; charset="iso-8859-1"
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
Fr?n: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] F?r Jason Rennie Skickat: den 11 januari 2011 14:11 Till: Twisted general discussion ?mne: Re: [Twisted-Python] client connecting to 2 servers (nonsimultaneously)
You could probably generalize and simplify by using a collections.deque of hosts/ports and using the rotate() method before each?reactor.connectTCP. ?Also, no need for multiple reactor imports---one at the top of the code is fine. ?Note that if the connection is "lost" in a non-clean fashion, you may also want to reconnect. ?IIUC, "fail" only handles the case that no connection is made (protocol is never created).
import collections from twisted.internet import reactor
class MyClientFactory(object): ?? ?protocol = MyClientProtocol ?? ?def __init__(self, hosts): ?? ? ? ?self.hosts = collections.deque(hosts) ?? ? ? ?reactor.callWhenRunning(reactor.connectTCP, self.hosts[0][0], self.hosts[0][1], self) ?? ? ? ?self.hosts.rotate(1) ?? ?def clientConnectionFailed(self, connector, reason): ?? ? ? ?reactor.callLater(2.0, connectTCP, self.hosts[0][0], self.hosts[0][1], self) ?? ? ? ?self.hosts.rotate(1)
factory = MyClientFactory([('host1', port1), ('host2', port2), ...]) reactor.run()
Cheers,
Jason
On Tue, Jan 11, 2011 at 5:17 AM, <benjamin.bertrand@lfv.se> wrote: Hi,
I'm new to twisted and I have started to write a new protocol with a TCP client and server. In my protocol, a client should be able to connect to 2 servers (master/slave node - only the master accepts connection). The client should try to connect to server1. If it fails, try to connect to server2 (after a specific timeout). If that fails, try server1... I came up with a solution (see below). As I'm new to twisted and I haven't seen anything like that in the examples, I'd like to check if that's a proper way to do it. Any comments is welcome.
Thanks
Benjamin
*********************************************** class MyClientFactory(ClientFactory):
? ?protocol = MyClientProtocol
? ?def __init__(self, host2=None): ? ? ? ?self.host1 = None ? ? ? ?self.host2 = host2
? ?def clientConnectionFailed(self, connector, reason): ? ? ? ?from twisted.internet import reactor ? ? ? ?if self.host2 is None: ? ? ? ? ? ?# host2 is not defined, reconnect to host1 ? ? ? ? ? ?reactor.callLater(2.0, connector.connect) ? ? ? ?else: ? ? ? ? ? ?destination = connector.getDestination() ? ? ? ? ? ?if self.host1 is None: ? ? ? ? ? ? ? ?# First connection failed, initialize host1, and try host2 ? ? ? ? ? ? ? ?self.host1 = destination.host ? ? ? ? ? ? ? ?host = self.host2 ? ? ? ? ? ?elif destination.host == self.host1: ? ? ? ? ? ? ? ?# Connection to host1 failed, try host2 ? ? ? ? ? ? ? ?host = self.host2 ? ? ? ? ? ?else: ? ? ? ? ? ? ? ?# Connection to host2 failed, try host1 ? ? ? ? ? ? ? ?host = self.host1 ? ? ? ? ? ?reactor.callLater(2.0, reactor.connectTCP, host, destination.port, self)
factory = MyClientFactory(server2) from twisted.internet import reactor reactor.connectTCP(server1, 8010, factory) reactor.run() ***********************************************
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Jason Rennie Research Scientist, ITA Software 617-714-2645 http://www.itasoftware.com/
------------------------------
Message: 2 Date: Wed, 12 Jan 2011 10:15:59 -0500 From: Jason Rennie <jrennie@gmail.com> Subject: Re: [Twisted-Python] client connecting to 2 servers (nonsimultaneously) To: Twisted general discussion <twisted-python@twistedmatrix.com> Message-ID: <AANLkTi=ge7JGPY3_woNu6LCapUEXjH88pRZ=TB05g5dY@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1"
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@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/
participants (1)
-
williem75@gmail.com