socketcluster-client-python icon indicating copy to clipboard operation
socketcluster-client-python copied to clipboard

Socket client is blocking the thread

Open valamorgon opened this issue 7 years ago • 2 comments

When I run the following code, everything works well but the last print("after line") line, not executes unless communication interrupted.

from socketclusterclient import Socketcluster
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

def onconnect(socket):
    logging.info("on connect got called")

def onConnectError(socket,err):
    logging.info(err)
    logging.info("err")

def ondisconnect(socket):
    logging.info("dc")

def onSetAuthentication(socket, token):
    logging.info("Token received " + token)
    socket.setAuthtoken(token)
        
def onAuthentication(socket, isauthenticated):
    logging.info("Authenticated is " + str(isauthenticated))
    socket.emit("sample","hi")
    
if __name__ == "__main__":
    socket = Socketcluster.socket("ws://localhost:8000/socketcluster/")
    socket.setBasicListener(onconnect, ondisconnect, onConnectError)
    socket.setAuthenticationListener(onSetAuthentication, onAuthentication)
    socket.connect()
    print("after line")

And in console I have the following lines and it is stuck

INFO:on connect got called
INFO:{"rid":1,"data":{"id":"EhStRAQxoM9SHSo2AAAG","pingTimeout":20000,"isAuthenticated":false}}
INFO:Authenticated is False
INFO:Emit data is {"data": "hi", "event": "sample"}

My server code is:

scServer.on('connection', function (socket) {

      socket.on('sample', function (data) {
        console.log('Handled sampleClientEvent', data);
      });

    });

And server console: Handled sampleClientEvent hi

valamorgon avatar Jan 04 '18 05:01 valamorgon

Hi @valamorgon, I think newtwork thread becomes active when you call connect method. That's why you cannot process any further. I will recommend using this strategy

from socketclusterclient import Socketcluster
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

def onconnect(socket):
    logging.info("on connect got called")

def onConnectError(socket,err):
    logging.info(err)
    logging.info("err")

def ondisconnect(socket):
    logging.info("dc")

def onSetAuthentication(socket, token):
    logging.info("Token received " + token)
    socket.setAuthtoken(token)
        
def onAuthentication(socket, isauthenticated):
    logging.info("Authenticated is " + str(isauthenticated))
    startCode(socket);

    
if __name__ == "__main__":
    socket = Socketcluster.socket("ws://localhost:8000/socketcluster/")
    socket.setBasicListener(onconnect, ondisconnect, onConnectError)
    socket.setAuthenticationListener(onSetAuthentication, onAuthentication)
    socket.connect()

def startCode(socket) :
    socket.emit("sample","hi")
    print("after line")

Or you can create a new thread and call connect method inside it. So, it won't block your main thread.

sacOO7 avatar Jan 04 '18 05:01 sacOO7

I tried to encapsulate the example into class which then called from another .py socket = SocketCluster() and on interval I planned to call socket.publish("Hi All") but since the main thread is blocked, how to do it properly/ safely ?

perkasajob avatar Sep 28 '18 16:09 perkasajob