分布式单词发音抓取机器人
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()
- ››分布式计算多机部署与配置
- ››分布式单词发音抓取机器人
- ››分布式网络爬虫关键技术分析与实现一网络爬虫相关...
- ››分布式 DBA: 创建和使用分区表
- ››分布式 Key-Value 存储系统:Cassandra 入门
- ››分布式 DBA: Cursor Stability Isolation Level 的...
- ››分布式 DBA:存储、I/O 和 DB2,第 1 部分(针对在...
- ››分布式管理:用 HADR 减少停机时间(详细解释如何...
- ››分布式 DBA:使用物化查询表
- ››分布式存储系统的实现
- ››分布式 DB2 UDB 服务器对比
- ››分布式DBA:掌握SQL存储过程
更多精彩
赞助商链接