I have application in which a server can send data to multiple clients. On server-side, I start 2 threads:
- accepting connections
- sending data to connected clients.
Sending data function:
def send_data(self):
'''this function is a thread which sends data to clients as Queue is filled by the on_move_up function'''
while True:
data=self.line_queue.get() # get data from queue
json_data=json.dumps(data)
for client in self.clients_stroke:
try:
client.send(json_data)
except socket.error as e: # if no socket if connected
# ... write code here to remove that client from socket
traceback.print_exc()
self.line_queue.task_done() # task completed
s.close()
This is not very scalable as it send data to clients sequentially. One way is I start a separate thread for each client. Which also will consume memory and not be scalable.
What are the other good options(, equivalent code if possible)?