[Twisted-Python] data update to multiple clients
Phil Mayers
p.mayers at imperial.ac.uk
Mon Apr 5 06:01:17 MDT 2010
On 04/05/2010 12:39 PM, Chris Cusack wrote:
> Probably another newbie question but after much effort I am not
> progressing on the following.
>
> I am trying to design a system where I have a microprocessor
> periodically feeding data on a serial connection to a server script. I
> would then like the server script to notify one or more client scripts
> of the changed data across a local network. I have tried a few simple
> tcp server and client script examples which communicate well for a
> single call but do not seem to suit my application which is not client
> event driven. I considered having each client connect to the server
> every ?? seconds to query for new data but there may be a better way.
Perhaps I'm misunderstanding, but it's pretty simple. Just have the
clients connect & stay connected. Keep a list of currently connected
clients somewhere (see below) and every time you receive data on the
serial port, write it out to all the clients. For example:
# ack, global variable!
client_list = []
class MyProtocol(Protocol):
def connectionMade(self):
client_list.append(self)
def connectionLost(self, reason):
if self in client_list:
client_list.remove(self)
def notifyClient(self, data):
# you will probably want some framing here e.g. Netstring, len+data
self.transport.write(data)
class ServerProto(Protocol):
def dataReceived(self, data):
for cli in client_list:
cli.notifyClient(data)
...or am I misunderstanding you?
More information about the Twisted-Python
mailing list