Reputation: 3596
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?
Upvotes: 0
Views: 119
Reputation: 156138
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.
Upvotes: 1