9

I have an autobahn Websocket Server with the typical onX functions in it's protocol. My problem is that I can't find a way to exit onX, while keep doing the various stuff that I wanted to do when the specific message arrived. More specifically in my onMessage function, I sometimes perform an HTTP request to an API which is very slow. As a result, the client that sent the websocket message is being blocked by the server's onMessage finalization. Even if I do self.sendMessage or reactor.callFromThread(<http request here>), or self.transport.loseConnection() from the server side, in the onMessage block, the onMessage is still executing the HTTP request and my client waits.

This is my client's code:

@asyncio.coroutine
def send(command,userPath,token):
    websocket = yield from websockets.connect('wss://127.0.0.1:7000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1_2))
    data = json.dumps({"api_command":"session","body":command,"headers": {'X-User-Path': userPath, 'X-User-Token': token}})
    response = {}
    try:
        yield from websocket.send(data)
    finally:
        yield from websocket.close()
    if 'command' in response:
        if response['command'] == 'ACK_SESSION_COMMAND' or response['command'] == 'ACK_INITIALIZATION':
            return ('OK',200)
        else:
            return('',400)

I even tried to just websocket.send(data), from the client, but for some reason it doesn't send the data (I don't see them arriving in the server). I don't understand how can I return from the onMessage block and keep doing my HTTP request.

And to explain my situation, I just want to sent 1 ssl websocket message to my server and immediately close the connection. Anything that can do that, suits me.

1 Answer 1

4

Using reactor.callInThread instead of reactor.callFromThread, causes the application flow to release and the HTTP request is performed independently in a thread. As described in the twisted documentation : http://twistedmatrix.com/documents/13.2.0/core/howto/threading.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.