简体   繁体   中英

Send large number of messages in python twisted

I have a twisted TCPServer that has a large number of client connections. I need to be able to have one master client issue a command to that server to send out messages to all the other client connections in a nonblocking manner. In other words, the server should have a command handler (built on top of lineReceived) that takes a special command from the master client, then launches a thread or process to loop over all the other clients and send a command (sendLine) to each of these clients. The command handler for the master client should return immediately after launching the thread. The problem is that any call to sendLine in twisted must be called from the main reactor thread, so it must be done using callFromThread. So is it possible for my master command handler to do something like this:

def handle_master_command(self,command):
    deferred=reactor.deferToThread(myfunc)
    return

def myfunc(self):
    for client in other_clients:
        reactor.callFromThread(send_stuff_to_client,client,stuff_to_send)

Obviously this is just pseudocode, but will something like this work?

er, LineReceiver.sendLine() is already non-blocking; you don't need any threads for this, just do:

def handle_master_command(self, command):
    for client in other_clients:
        send_stuff_to_client(client, stuff_to_send)

If any twisted api says that it "must be called from the reactor thread", that always means its going to be non-blocking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM