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!