0

I have application in which a server can send data to multiple clients. On server-side, I start 2 threads:

  1. accepting connections
  2. 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)?

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • I believe there are too many options for this, consider looking into gevent, Twisted or asyncio. – Elias Dorneles Mar 20 '16 at 00:50
  • @elias I don't like gevent, twisted is complicated. I guess I am looking for an asyncio solution. – Abhishek Bhatia Mar 20 '16 at 00:55
  • asyncio is just as much complicated as core twisted. why exactly you don't like gevent? consider if it is a technical matter or a psychological one. :) – Elias Dorneles Mar 20 '16 at 01:04
  • It's a little bit incorrect to say that the posted code "sends to each client sequentially". In fact, send() returns as soon as the passed-in data is copied over to the kernel's outgoing-data-buffer for that socket, and in particular send() does not block waiting for the data to arrive at the emote host. So even with a single thread and blocking sends you will still be sending data to multiple clients simultaneously (how many depends on the size of your sockets' SO_SNDBUF buffers) – Jeremy Friesner Mar 20 '16 at 02:30
  • That said, if you want better control without creating a thread per socket, you can set each socket to non-blocking and handle them all in a single thread via select() or poll(). Note that you will need to maintain your own outgoing-data queue for each socket in this case, to store data that you want to send to that socket but can't (yet). – Jeremy Friesner Mar 20 '16 at 02:32

0 Answers0