1

I'm making a threaded chat server and I need a way to send a message to all the clients. I could use a global queue but then only one of the threads handling the clients would be able to send the message. So I was wondering if its possible to create a separate queue object within each of the client threads and append them to a list so that I would be able to send the message to each client's queue. Is this possible?

clientqueues = [] #Global list of client queues

class ClientThread(threading.Thread):
    def __init__(self):
        myqueue = Queue.Queue() #Client queue
        clientqueues.append(myqueue)
        ...
def MessageAllClients(message):
    global clientqueues
    for queue in clientqueues:
        queue.put(message)

Would this work or am I going about this the wrong way?

Wichid Nixin
  • 287
  • 1
  • 4
  • 14

2 Answers2

3

Your approach is just fine. The only thing I would change is making clientqueues a static member of ClientThread rather than a global variable.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

A Queue is just an object (like everything in Python), so no problem with making a list of them.

pawroman
  • 1,270
  • 8
  • 12