[Twisted-Python] processProtocol woes and a plea for aid

Phillip J. Eby pje at telecommunity.com
Wed Jun 23 14:33:51 EDT 2004

At 01:34 PM 6/23/04 -0400, Gary Coulbourne wrote:
>I'm using processProtocol to talk to a program written by a coworker.  It 
>handles things fine, but it is asynchronous in that when I send a response 
>to it, it may send several responses back, or it may just randomly send 
>back a response.  If I were handling this output a line at a time, it 
>would be fine, but processProtocol seems to put all of the responses 
>together... each response is followed by a \n.  I naively assumed that 
>each time a line was complete, Twisted would call my outRecieved.  It 
>seems, though, that dozens of respopnses can build up in the buffer before 
>it was called.
>I'm certain I am missing out on something basic.  Is there any way to set 
>processProtocol to look at the \n as a signal to call outRecieved rather 
>than to call it at some arbitrary time?  I didn't see anything quickly 
>obvious in the source, so I thought I would ask for advice.

It sounds like maybe the program you're calling isn't flushing its writes 
to stdout.  If it doesn't, there's no way for Twisted to see its output 
until the program's output buffer overflows.

One quick way to check: are you always getting the messages in blocks of 
roughly N bytes, (except perhaps for the last block before the program 
exits)?  If so, you need to get your coworker to flush his output buffer.

Alternatively, depending on your platform, creating a pty (aka 
pseudoterminal) may be an option.  This would be to convince your 
co-worker's program that it's talking to a console, and therefore should 
output data as it becomes available instead of buffering it.  But that 
depends somewhat on the language/libraries being used by the program.

More information about the Twisted-Python mailing list