WEB开发网
开发学院服务器负载均衡 分布式单词发音抓取机器人 阅读

分布式单词发音抓取机器人

 2012-05-24 09:22:06 来源:WEB开发网   
核心提示:文件侦听线程 class FileStoreThread(threading.Thread): def __init__(self,wordsDownloaded): threading.Thread.__init__(self) self.fileSocket = socke

文件侦听线程 

class FileStoreThread(threading.Thread): 
 
    def __init__(self,wordsDownloaded): 
        threading.Thread.__init__(self) 
        self.fileSocket = socket(AF_INET,SOCK_STREAM) 
        self.fileSocket.bind(('',fileSocketPort)) 
        self.fileSocket.listen(maxConnectNum) 
        self.wordsList = wordsDownloaded 
 
    def run(self): 
        while True: 
            clientSock,address = self.fileSocket.accept() 
            print 'connect from %s is established.' % str(address) 
            fileWorker = FileServiceThread(clientSock,self.wordsList) 
            fileWorker.start() 

说明:在该类的构造函数中,将创建侦听套接字,并将已下载的单词列表作为类的参数成员。线程运行为一个死循环,其中启动accept方法,收到任务请求线程的请求之后,将启动写磁盘线程为其服务。
文件侦听线程状态转换图如图3所示:

李超

任务分发线程

class WordServiceThread(threading.Thread): 
     
    def __init__(self,clientSocket,wordsList):   
        threading.Thread.__init__(self) 
        self.clientSocket = clientSocket 
        self.wordsList = wordsList 
 
    def run(self): 
        global wordsListMutex 
        while True: 
            request = self.clientSocket.recv(maxWordLength) 
            requestStr = request.decode('ascii') 
            if(requestStr == 'please'): 
                tmpWordsList = '' 
                wordsListMutex.acquire() 
                for i in range(0,taskGrain): 
                    tmpWord = self.wordsList.front() 
                    tmpWordsList = tmpWordsList + ' ' + tmpWord 
                wordsListMutex.release() 
                self.clientSocket.send(tmpWordsList) 
            else: 
                self.clientSocket.close() 
                break 

说明:在该类的构造函数中得到为请求线程服务的套接字,以及全部单词列表的队列。在run中执行任务发送协议。首先受到一个请求字符串,应该为‘please’,否则表示应该在服务器端关闭为其服务的套接字。
收到please之后,从所有单词队列中取出一定数量的单词拼成一个字符串发给送从节点的请求线程。
该线程的执行逻辑对应的流程图如图4所示:

李超

写磁盘线程

class FileServiceThread(threading.Thread): 
    global soundQueueMutex 
    def __init__(self,clientSocket,wordsDownloaded): 
        threading.Thread.__init__(self) 
        self.socket = clientSocket 
        self.wordsDownloaded = wordsDownloaded 
 
    def run(self): 
        while True: 
            rawSaveName = self.socket.recv(maxWordLength)        
            word = rawSaveName.decode('ascii') 
            if(word == ''): 
                continue 
            if(word == '0'): 
                break 
            saveName = word + '.mp3' 
            outfd = open(saveName,'wb')  
            helloPacket = 'OK' 
            helloPacketStr = helloPacket.encode('ascii') 
            self.socket.send(helloPacketStr) 
            while True: 
                data = self.socket.recv(dataBlockSize) 
                outfd.write(data) 
                query = 'any more?' 
                self.socket.send(query.encode('ascii')) 
                answer = self.socket.recv(maxWordLength) 
                answer = answer.decode('ascii') 
                if(answer == 'yes'): 
                    continue 
                else: 
                    break 
            print 'received %s successfully...' % saveName 
            outfd.close() 

上一页  1 2 3 4 5  下一页

Tags:分布式 单词 发音

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接