[Twisted-Python] ReconnectingClientFactory creates multiple Protocols?

Itamar Shtull-Trauring itamar at itamarst.org
Thu Sep 15 08:27:08 MDT 2005


On Thu, 2005-09-15 at 13:32 +0800, mikah at ceruleansoftware.com wrote:

>   As far as I can tell, it's related to the connection being
> dropped, at which point the Factory reconnects and creates a
> new protocol instance.

Protocols (at least, when used with TCP) are designed so that their
lifetime matches that of the TCP connection they are handling. As a
result, when the connection is lost and the factory reconnects, this is
a new TCP connection with a new protocol. This is why there's a factory,
to manage data and logic that is not tied to a specific TCP connection
(e.g. "should I reconnect?" or "how far along was the download when I
got disconnected.")

In general you'd want to use the factory to store state that needs to
last past the lifetime of the protocol. You can, of course, have
buildProtocol always return the same instance, but that's pretty ugly
and can easily lead to obscure bugs if you don't clean up the state
correctly.





More information about the Twisted-Python mailing list