[Twisted-Python] CONNECTION_LOST not an integer (docstring error?)

glyph at divmod.com glyph at divmod.com
Wed Oct 1 17:40:59 EDT 2008

On 09:24 pm, mforbes at physics.ubc.ca wrote:
>Why does writeSomeData not simply raise CONNECTION_LOST as an 
>exception? Checking return values is quite un-pythonic.  Is there a 
>deep reason for this?

writeSomeData is an internal API that nobody should really use, so it's 
not factored for convenience; it's also deep down in the guts of the 
reactor's innermost loop, so it pays a lot of attention to efficiency. 
Raising exceptions is really, really slow, at least by the standards of 
that kind of code.
>P.S. I came across this because I was trying to use twisted running  in 
>a thread to write data resulting from a long computation that I  have 
>not yet turned into a producer.  The more conventional "write"  method 
>was failing if the socket backed up, and provided no simple  way of 
>determining if data was being dropped.  My solution was to  used 
>writeSomeData, and then have the computation decide to throw out  some 
>of the data if it is being produced too rapidly, but I need to  know 
>how much has been sent so I can decide what to throw away...

You do know that Twisted APIs are not thread safe, right?  You can't 
call write() from a thread?

More information about the Twisted-Python mailing list