[Twisted-Python] 3 basic questions about connectionLost, reactor stop and design

Itamar Shtull-Trauring itamar at itamarst.org
Mon Apr 2 07:51:24 EDT 2007

On Mon, 2007-04-02 at 11:19 +0800, 甜瓜 wrote:
> Hi all,
> Expecting your kind answer regardless how primary my questions are.  ^_^
> Q1: Protocols has a method: connectionLost(self, reason). What is the
> type of the second parameter? I see it sometimes is
> twisted.internet.error.XXX. But who pass this param?

It's typically a twisted.python.failure.Failure wrapping, as you said,
t.i.error.ConnectionLost or ConnectionDone. It's called by the reactor.

> Can I break a connection by passing my own 'reason'?

That is not supported, no.

> What is the proper place to put reactor.stop()? Is there any other way
> to normally exit a twist program?

reactor.stop() is the way to go, yes. Call it when you want the program
to start.

> Finally, my question is why twisted use class attribute so widely? Is
> there any benefit? In my opinion, __init__ function is the only good
> place to define 'attributes'. But
> twist distributes 'attributes' in two forms: class and __init__. I
> want to know the idiom to decide which attribute should be put into
> which part.

I don't think there's any specific reason why we use one or the other
(though one should never use mutable ones in this way.) If you're
happier always using __init__ then by all means do so.

More information about the Twisted-Python mailing list