Question

I am actually using a pyopengl program to act as a socket server. At the same time the server receives commands from clients and interprets these commands and does corresponding drawing at the same time.

In case that the main thread got blocked when doing socket things I actually started a thread to do the socket server accept thing, and the updateserver socket method is called in the display loop.

class SocketServer(Thread):
def __init__ (self ):
    Thread.__init__(self)
    self.serversocket = socket(AF_INET, SOCK_STREAM);
    self.serversocket.bind(("127.0.0.1", 7780));
    self.status = -1;#not connected
    self.clientsocket         = None;
    self.clientaddress        = None;
    self.clientbuffer         = None;
    self.serversocket.listen(5);
def run(self):
    print 'thread running'
    while( self.status == -1 ):
        time.sleep(10);
        (self.clientsocket, self.clientaddress) = self.serversocket.accept();
        self.clientbuffer = self.clientsocket.makefile('r',0);
        self.status = 0;
    #Thread.kill();
    #print 'SERVER LISTENNING FINISHED';

def getClientContent(self):
    if ( not self.clientbuffer ): return "NONE CLIENT BUFFER";
    return self.clientbuffer.readline();

ss = SocketServer();

def updateServerSocket():
    global ss;
    print ss.getClientContent();
  1. I don't know how to terminate the thread
  2. once the client written in another python file started to push data, the display loop got hangup as if it were dead
Was it helpful?

Solution

In getClientContent, the line self.clientbuffer.readline(); is doing a blocking call on the socket, and that is almost certainly what is causing your trouble.

What you need to do instead is have your run method do the readlines and stick the data into a queue, which the graphical display thread then reads from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top