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

甜瓜 littlesweetmelon at gmail.com
Sun Apr 1 23:19:05 EDT 2007

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?

Can I break a connection by passing my own 'reason'?
The method transport.lostConnection is disappointment to me because I
cannot pass any parameter to it to indicate the reason.

Q2: How to cleanly exit a twist program?
Eg: When a twisted client called transport.lostConnection or cut off
net wire physically, only the connection is broken. The reactor is
still running in the event loop.  -_-!!

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

Q3: Let's continue my previous question about 'class attribute'. Phil
Mayers gave the key to me:
'if you then modify an instance, the class and
other instances are not, and those modifications OVERRIDE ANY
CONFLICTING ones on the class.'
class A:
    i = 0
p = A()
q = A()
p.i = 1
p.__dict__        # member varible added
>>> {'i': 1}
p.__class__.i = 2 # explicit access to A.i
>>> 2             # shared A.i
Therefore, in my opinion, the class attribute A::i is always shared by
instances. Different from C++, python 'p.i = 1' will add {'i': 1} to
p.__dict__, which makes 'i' as the

'member varible' to instance 'p'. It seems 'getattr' prefers 'member
variable' than 'class attribute'. So later access to 'p.i' will refer
to 'member variable', and the 'class

attribute' is masked. But if I use p.__class__.i explicitly, the
masked 'class attribute' appears. Till now, Have I misunderstood
anything? ^_^

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.

Thank you for your patience!


More information about the Twisted-Python mailing list