Raspberry Pi b上的Python线程锁+

2024-09-30 06:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python相当陌生(对python3更是如此),但我不知道这里有什么问题。 我的代码相当简单,运行在raspberryb+上。 它基本上与VLC的socket接口通信。下面是线程的代码:

    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

它是这样叫的:

^{pr2}$

只是想让你知道。 我的客户机每隔300毫秒向Python服务器发送命令,以便检索播放的曲目的位置等等。 会发生的情况是,有些线程只是挂起消耗100%CPU的内存,就像它被卡在一个循环中一样(在X分钟或数小时后,它确实是可变的)。但是我绝对没有出现异常,我想知道在Python解释器中发生的情况是否比脚本本身更严重。 它可以完美地工作在我的桌面和任何其他x86_64CPU上,具有正常的资源(i3到i7和超过2GB的RAM)。 我觉得这个问题更多的是由于Python不能很好地处理低资源,而不是我的脚本,但是如果有人能告诉我,如果我做了什么明显的错误,那将是我的一天。 谢谢!


Tags: andtoselfcmdlenifloggingsocket

热门问题