class MuseAPI(object): def __init__(self, host): self.sock = MySocket() self.sock.client(host) self.lock = RLock() self.muse = MuseData() self._muse = MuseData() produce = Thread(target=self.handler) produce.daemon = True produce.start() def subscribe(self, k): self.sock.send('SUB ' + str(k)) def unsubscribe(self, k): self.sock.send('UNS ' + str(k)) def stop(self): self.sock.send('END') self.sock.sock.close() def update(self): with self.lock: self.muse.__dict__.update(enumerate(self._muse.__dict__)) def handler(self): while 42: try: data = self.sock.recv() except error: break with self.lock: self._muse.update(data) print('Close')
from MySocket import MySocket from time import time, sleep data = ['This Is a A data', 'This Is a B data', 'This Is a C data'] s = MySocket() s.client(('localhost', 5000)) while 42: for d in data: s.send(d + str(time()))