views:

80

answers:

4

Is there any way to force self.transport.write(response) to write immediately to its connection so that the next call to self.transport.write(response) does not get buffered into the same call.

We have a client with legacy software we cannot amend, that reads for the 1st request and then starts reading again, and the problem I have is twisted joins the two writes together which breaks the client any ideas i have tried looking into deferreds but i don't think it will help in this case

Example: self.transport.write("|123|") # amount of messages to follow a loop to generate next message self.transport.write("|message 1 text here|")

expected |123| |message 1 text here|

Result: |123||message 1 text here|

+1  A: 

Put a relatively large delay (10 seconds) between writes. This will be the only possible solution. Because if the recipient is so badly written by people who don't know what TCP is and how to use it, you can hardly do anything (other than rewrite that application).

Eugene Mayevski 'EldoS Corp
Thank you for your comment, sadly still the same issue. It appears twisted waits until all processing is finished even with a time.sleep(60) before sending traffic down the line. I am going to have to look at some other implementation to get this to work :(
Jarratt
The reactor waits until you let it run to do anything. If you don't let it run, it can't do anything. time.sleep(60) prevents it from running. If you want to do something else after 60 seconds, use `reactor.callLater(60, somethingElse)`
Jean-Paul Calderone
+1  A: 

Looks like you need to use inline callbacks, as described in the accepted answer here: http://stackoverflow.com/questions/776631/using-twisteds-twisted-web-classes-how-do-i-flush-my-outgoing-buffers

Bob
A: 

Can you tell which transport you are using. For most implementations, This is the typical approach :

 def write(self, data):
        if data:
            if self.writeInProgress:
                self.outQueue.append(data)
            else:
                ....

Based on the details the behavior of write function can be changed to do as desired.

pyfunc
Currently i am using Twisted SSL to create a secure server
Jarratt
A: 

Hi,

I think i found the answer after digging through the code i found doWrite() and calling

self.transport.doWrite() directly after the self.transport.write() flushes the buffer allowing me to loop and start writing the additional data.

Thank you everybody that took the time to assist me.

Jarratt
You should never call doWrite yourself. This is unsupported, unportable, and unnecessary. You just have to let the reactor run once in a while.
Jean-Paul Calderone