0

I am having problems with twisted.internet.reactor All my clients have completely identical environments, but only some experience this problem:

They correctly connectTCP to the server via ws and exchange first several messages. About one minute in, they should send a message to the server via

def execute(self, message, callback=None):
    print(">>>", message, flush=True)
    reactor.callFromThread(self._client_protocol_instance.send, message, callback)

self._client_protocol_instance.send method is defined as follows:

def send(self, command, callback):
    print("send", command, callback, flush=True)
    timestamp = int(time() * 1000000)
    msg = (command.strip() + " --timestamp:" + str(timestamp))
    if _self._debug:
        _self._commands[str(timestamp)] = msg
    if callback is not None:
        _self._callbacks[str(timestamp)] = callback
    payload = msg.encode()
    _self._status_controller.set_state(payload)
    self.sendMessage(payload)

First print shows up in stdout, but second one doesn't. I assume that send doesn't get executed. After reactor.run(), this is the only reference to the reactor in the entire program.

Killing client's process after this happens is immediately detected by the server, so the connection was still alive at that time.

What could be causing this?

Mirac7
  • 1,566
  • 4
  • 26
  • 44

1 Answers1

0

I found the solution, the problem lied with the fact that the previous task wouldn't finish sometimes by the time it tried to send the message.

I solved it by moving all cpu-heavy response handling logic into threads to free up the reactor for other messages.

Mirac7
  • 1,566
  • 4
  • 26
  • 44