[Twisted-Python] Twisted-Python Digest, Vol 82, Issue 12

williem75 at gmail.com williem75 at gmail.com
Thu Jan 13 08:29:06 MST 2011


Sent from my LG phone

twisted-python-request at twistedmatrix.com wrote:

>Send Twisted-Python mailing list submissions to
>	twisted-python at 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 at twistedmatrix.com
>
>You can reach the person managing the list at
>	twisted-python-owner at 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 at lfv.se)
>   2. Re: client connecting to 2 servers	(nonsimultaneously)
>      (Jason Rennie)
>   3. Re: client connecting to 2	servers	(nonsimultaneously)
>      (exarkun at 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 at lfv.se>
>Subject: Re: [Twisted-Python] client connecting to 2 servers
>	(nonsimultaneously)
>To: <twisted-python at twistedmatrix.com>
>Message-ID: <2C9A58914594E34AA28179DB776FABDF92E4 at 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 at twistedmatrix.com [mailto:twisted-python-bounces at 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 at 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 at 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 at gmail.com>
>Subject: Re: [Twisted-Python] client connecting to 2 servers
>	(nonsimultaneously)
>To: Twisted general discussion <twisted-python at twistedmatrix.com>
>Message-ID:
>	<AANLkTi=ge7JGPY3_woNu6LCapUEXjH88pRZ=TB05g5dY at 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 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: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110112/74ab6e3d/attachment.html 
>
>------------------------------
>
>Message: 3
>Date: Wed, 12 Jan 2011 15:26:37 -0000
>From: exarkun at twistedmatrix.com
>Subject: Re: [Twisted-Python] client connecting to 2	servers
>	(nonsimultaneously)
>To: Twisted general discussion <twisted-python at twistedmatrix.com>
>Message-ID:
>	<20110112152637.1811.1981888275.divmod.xquotient.9 at localhost.localdomain>
>	
>Content-Type: text/plain; charset="utf-8"; format="flowed"
>
>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
>
>
>
>------------------------------
>
>Message: 4
>Date: Wed, 12 Jan 2011 10:52:05 -0500
>From: Jason Rennie <jrennie at gmail.com>
>Subject: Re: [Twisted-Python] client connecting to 2 servers
>	(nonsimultaneously)
>To: Twisted general discussion <twisted-python at twistedmatrix.com>
>Message-ID:
>	<AANLkTimRg4xWrjwPQLwqVUabOAtJLwJ=QUvLYAVYSBiH at mail.gmail.com>
>Content-Type: text/plain; charset="iso-8859-1"
>
>Benjamin, sorry for providing you with an incorrect explanation.  Jean-Paul,
>thank you for correcting my broken understanding of callWhenRunning.
>
>Is there a call which puts a function into the reactor's queue of tasks to
>be completed?
>
>Thanks,
>
>Jason
>
>On Wed, Jan 12, 2011 at 10:26 AM, <exarkun at twistedmatrix.com> wrote:
>
>> 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
>>
>>
>-- 
>Jason Rennie
>Research Scientist, ITA Software
>617-714-2645
>http://www.itasoftware.com/
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110112/45f97e25/attachment-0001.htm 
>
>------------------------------
>
>Message: 5
>Date: Wed, 12 Jan 2011 11:32:55 -0500
>From: Glyph Lefkowitz <glyph at twistedmatrix.com>
>Subject: Re: [Twisted-Python] client connecting to 2 servers
>	(nonsimultaneously)
>To: Twisted general discussion <twisted-python at twistedmatrix.com>
>Message-ID:
>	<AANLkTinoeNJnLTgBSj5bL1zm2cwC8N5Kk18+73TiTFot at mail.gmail.com>
>Content-Type: text/plain; charset="utf-8"
>
>On Wed, Jan 12, 2011 at 10:52 AM, Jason Rennie <jrennie at gmail.com> wrote:
>
>> Benjamin, sorry for providing you with an incorrect explanation.
>>  Jean-Paul, thank you for correcting my broken understanding of
>> callWhenRunning.
>>
>> Is there a call which puts a function into the reactor's queue of tasks to
>> be completed?
>>
>
>The reactor doesn't have a queue of tasks to be completed.  It has sets of
>various event sources, which it executes in no particular order.
>
>Scheduling a timed event with callLater(0,...) might do what you want,
>though.
>-------------- next part --------------
>An HTML attachment was scrubbed...
>URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110112/5d2959bd/attachment-0001.htm 
>
>------------------------------
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>End of Twisted-Python Digest, Vol 82, Issue 12
>**********************************************


More information about the Twisted-Python mailing list