Raspberry Pi b +上的Python线程锁定

时间:2016-02-21 10:57:49

标签: python multithreading python-3.x

我对Python很新(甚至更多的是Python 3),但我无法弄清楚这里的问题是什么。 我的代码非常简单,可以在Raspberry B +上运行。 它基本上与VLC的套接字接口通信。这是线程的代码:

    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

这就是它的名字:

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()

只是为了让你知道。 我的客户端每300毫秒向Python服务器发送一次命令,以便检索播放的曲目的位置等等。 发生的事情是,有些线程只是占用了100%的CPU,就像它被卡在一个循环中一样(在X分钟或几小时之后,它真的是可变的)。但我绝对没有提出异常,我想知道Python解释器中是否发生了比脚本本身更多的事情。 它可以在我的桌面和任何其他具有正常资源的x86_64 CPU(i3到i7和超过2 Gb的RAM)上完美运行。 我觉得这个问题更多的是由于Python不能很好地处理低资源而不是我的脚本,但是如果有人能告诉我,如果我做的事情显然是错误的,它将真正成为我的一天。 谢谢!

0 个答案:

没有答案
相关问题