I have a python program that currently uses a tcp/ip client module I wrote to receive data from a streaming server. The server outputs lines of data.
My TCP client class is fairly primitive and I want to refactor to use a twisted ReconnectingClientFactory.
The main program currently gets data from a readLines function in my TCP Client that 'yields' the lines as they are received.
The TCP client method is accessed by:
for msg_buffer in self.myTcpClient.readLines():
do some stuff with the data in msg_buffer
In my TCP Client the readLines method in essence looks like:
while True:
newLine = self.sock.recv(self.buffer_size)
yield newLine
When I implement the twisted client I'll want some way for it to behave as an iterator and yield data. I assume I'd do something in the protocol dataReceived method.
I'm lost trying to figure out how this works. It appears to me that twisted deferred are meant for this sort of use but I can't figure out how to use a deferred for my purpose (if my assumption about deferred is correct).
In a perfect world the twisted client would yield the lines as received so a call similar to the present method would do the job. i.e.
class GetData(protocol):
def dataReceived(self, data):
yield data
But I think that's an oversimplification.
In summary, what I'm trying to do is implement a twisted reconnecting TCP client that behaves something like my readLines method and can be accessed more or less like:
for msg_buffer in self.twistedTcpClient.readLines():
Any pointers will be much appreciated
UPDATE: I just stumbled across 'Crochet' for twisted. At first glance Crochet appears to have been designed for exactly the kind of model that I need... I'll report back after some testing