[Twisted-Python] A few simple questions

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Jul 17 16:02:22 EDT 2012


On 04:25 pm, rowen at uw.edu wrote:
>In article
><CAE_Hg6bDAiL_HUF1D7ZfigGv-FgXTDqsriq2RT44tAqweiBBDg at mail.gmail.com>,
>Laurens Van Houtven <_ at lvh.cc> wrote:
>>On Thursday, July 12, 2012, Russell E. Owen wrote:
>>
>> > I'm adding Twisted support to some existing communication code and I
>> > have a few questions which I hope are simple. I've been reading the 
>>API
>> > documentation, but if the answers are there I'm missing them.
>> >
>> > What happens if one tries to write to a protocol.transport that is 
>>not
>> > connected or in an error state? (I'm hoping that this case has 
>>defined
>> > behavior and that it will raise an exception).
>>
>>
>>Not connected usually means that protocol.transport is None
>>
>>
>> >
>> > Is there some way to query a protocol to see if it is in a good 
>>state
>> > (connected and no errors)? I realize with callbacks this is not 
>>usually
>> > necessary, but I have a case where it would be helpful (though I can
>> > probably manage without it).
>>
>>Can you elaborate on the case?
>
>I've found that Twisted sometimes swallows errors unless I am extremely
>careful. I would like to be able to check a protocol to make sure it is
>operational (connected and happy) as a means of assuring that I've not
>missed an error.

Instead, make sure you add errbacks to all your Deferreds.

Twisted does not swallow errors.  However, a Deferred (that your 
application keeps a reference to, preventing it from being garbage 
collected) that has a Failure result and no more errbacks will sit on 
that Failure until you let the Deferred get garbage collected or add an 
errback to the Deferred.

Jean-Paul
>> > When reading raw bytes, is there any way to get the bytes from the
>> > protocol? I assume not: that if one wants to buffer the data one 
>>must do
>> > that in the callback. If so, does Twisted provide a suitable buffer
>> > class?
>>
>>The protocol gets dataReceived called. What kind of buffering do you 
>>want
>>to do and why? Would a simple StringIO suffice?
>
>I read the LineReceiver code (should have done that before asking) and
>realize it's buffering using a simple string. I just rewrote that 
>simple
>class to meet my needs and am quite happy.
>
>-- Russell
>
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python



More information about the Twisted-Python mailing list