1

I am fairly new to Python (and even more to Python 3) but I can't figure out what is the problem here. My code is fairly simple and run on Raspberry B+. It basically communicates with the socket interface of VLC. Here is the code of the thread:

    class MyClientConn(threading.Thread):
def __init__(self, clientConn, ServerPath):
    threading.Thread.__init__(self)
    self.clConn = clientConn
    self.clConn.settimeout(5.0)
    self.ServPath = ServerPath
    self.daemon = True
    self.start()


def run(self):
    self.clConn.send(b'Greeting\n')
    try:
        tmpb = b''
        Connected = True
        Cmd = b''

        while (Connected) and (Cmd.rfind(b'\n') == -1):  #Retrieve command sent from client
            try:
                tmpb = self.clConn.recv(1)
                if len(tmpb) > 0:
                    Cmd += tmpb
            except Exception as inst:
                Connected = False
                Cmd = b''
                return


        if Cmd == b'VLCcmd\n': #We need to pass a command to VLC
            try:
                VLCcmd = bytearray()
                tmpb = b''
                Connected = True
                while (Connected) and (VLCcmd.rfind(b'\n') == -1): #We retrieve the command to send to VLC
                    tmpb = self.clConn.recv(1)
                    if len(tmpb) > 0:
                        VLCcmd.extend(tmpb)
                    if len(tmpb) == 0:
                        Connected = False

                vlcSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create socket to communicate with VLC
                vlcSock.settimeout(2.0)
                vlcSock.connect(('127.0.0.1',31514))

                VLCrep = b''
                tmpb = b''
                Connected = True
                while (Connected) and (VLCrep.rfind(b'> ') == -1): #Clear VLC Welcome message
                    tmpb = vlcSock.recv(1)
                    if len(tmpb) > 0:
                        VLCrep += tmpb
                    if len(tmpb) == 0:
                        Connected = False

                vlcSock.send(VLCcmd) #Send command to VLC

                Connected = True
                VLCrep = b''
                tmpb = b''
                while (Connected) and (VLCrep.rfind(b'> ') == -1): #read VLC answer
                    tmpb = vlcSock.recv(1)
                    if len(tmpb) > 0:
                        VLCrep += tmpb
                    if len(tmpb) == 0:
                        Connected = False

                self.clConn.send(VLCrep + b'\n') #send VLC answer to client
                if (VLCcmd.find(b'get_time') == -1) and (VLCcmd.find(b'get_title') ==-1) and (VLCcmd.find(b'get_length')==-1) and (VLCcmd.find(b'playlist 2')==-1):
                    logging.debug('VLC Command: ' + VLCcmd.decode())
                    logging.debug('VLC Answer: ' + VLCrep.decode())
            except Exception as inst:
                logging.debug('VLCcmd error: ')
                logging.debug(inst)
            finally:
                if 'vlcSock' in locals():
                    vlcSock.close()
                Cmd = b''
                return

    except Exception as inst:
        logging.debug('Error in Run: ')
        logging.debug(inst)
    finally:
        self.clConn.close

And this is how it's called:

print ('Server path: ' + ServPath)

# Before to open a passive socket we check VLC socket is open

if CheckVLCI('127.0.0.1',31514):
        print('VLC running, all good :)')
else:
        print ('Could not connect to VLC. Exiting.')
        raise SystemExit

TCPServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCPServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print ('Server socket created...')
TCPServer.bind((host, port))
if host == '':
    print ('Server binded to interface: 0.0.0.0' + ' port: ' + str(port))
else:
    print ('Server binded to interface: ' + host + ' port: ' + str(port))

TCPServer.listen(128)

while 1:
    try:
        conn, addr = TCPServer.accept()
        MyClientConn(conn, ServPath)
    except Exception as inst:
        logging.debug('Main error:')
        logging.debug(inst)
    time.sleep(0.1)
TCPServer.close()

Just to let you know. My client sends commands every 300 ms to the Python server in order to retrieve the position of the track played and so on. What happens is that some threads just hang consumming 100% of CPU like if it was stuck in a loop (after X minutes or hours, it's really variable). But I have absolutly no exception raised and I am wondering if it's not happening in the Python interpreter more than the script itself. It works perfectly on my desktop and any other x86_64 CPU with normal ressources (i3 to i7 and more than 2 Gb of RAM). I have the feeling that the problem is more due to Python that doesn't cope well with low ressources than my script, but if anyone could tell me if I am doing something obviously wrong it will really make my day. Thanks!

lvlvvvt
  • 11
  • 1
  • If I understand your code correctly, you are opening a new socket everytime a VLCcmd is entered, which can be ever 300ms? In general there is a lot of logic involved. I'd try to separate stuff that needs to be done only once from stuff which really needs to be done every 300ms. To check the runtime of your program, see http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script, though it will be advantageous to refcator your code into more separate functions for that. – Jan Christoph Terasa Feb 22 '16 at 05:26
  • @Christoph Terasa you have got it right, the commands get_time and get_title and get_length are sent with 300 ms which is really the maximal value if I want to be able to display the position of the track in seconde. There are a lot of other commands that wouldn't be sent with that interval, all the commands from an event on client side (play, next, pause and so on) and I didn't put in the code all the parts to upload the tracks and so on for more clarity and because I know the problem lay in this part of the code. So would you say that to create a new socket every times is too heavy? – lvlvvvt Feb 22 '16 at 09:32

0 Answers0