[Twisted-Python] 3 basic questions about connectionLost, reactor stop and design
{'i': 1}
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
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 p.__class__.i = 2 # explicit access to A.i q.i 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! -- ShenLei
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.
Itamar Shtull-Trauring wrote:
reactor.stop() is the way to go, yes. Call it when you want the program to start.
To be able to do that you would have to recall John and George from heaven, reform the Beatles, pay them a lot to compose "Stop Me Down", and use that as the sound theme. -- Nicola Larosa - http://www.tekNico.net/ We need to hug authority into surrender. [...] The edifice of power is only the illusion of power once we realise that the power lies with us. -- David Icke, November 2006
On 4/2/07, Nicola Larosa <nico@teknico.net> wrote:
Itamar Shtull-Trauring wrote:
reactor.stop() is the way to go, yes. Call it when you want the program to start.
To be able to do that you would have to recall John and George from heaven, reform the Beatles, pay them a lot to compose "Stop Me Down", and use that as the sound theme.
Maybe it's win32 specific feature .) -- MS
甜瓜 wrote: [...]
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.
No particular reason, but here are some small reasons for using class attributes: * their presence (and initial value) is visible in the declaration of the class, rather than requiring reading the code of the __init__ method; * as a result, the variables (and initial values) will be automatically listed by tools like epydoc and pydoctor (if the author forgets to explicitly describe these attributes in the docstrings); * they require less typing from the code author than an assignment to "self.foo" in __init__; * if the class doesn't have an __init__ method yet, it saves even more typing to use a class variable than to add an __init__ just to set an instance variable; * they slightly reduce memory consumption. I'm not sure these reasons are good enough to compensate for the confusion this idiom can cause, but they're the ones I can think of. I suspect in Twisted's case the main reasons are the "less typing" ones rather than anything else. -Andrew.
participants (5)
-
Andrew Bennetts
-
Itamar Shtull-Trauring
-
Marian Schubert
-
Nicola Larosa
-
甜瓜