Python3,多線程,多客戶端,廣播數據
#!/usr/bin/env python3import timeimport threadingimport queueimport socket# 三個線程:# 線程1:產生遞增的數字,轉成字符串放到隊列中# 線程2:監聽端口,將產生的連接放到列表中# 線程3:從隊列中取出數字,遍歷連接列表,發送到所有客戶端# 線程1:產生遞增的數字,轉成字符串放到隊列中class PRoducer(threading.Thread): def __init__(self, work_queue): super().__init__() # 必須調用 self.work_queue = work_queue def run(self): #print("Begin produce...") num = 1 while True: self.work_queue.put(str(num)) num = num+1 time.sleep(1) # 暫停1秒# 線程2:監聽端口,將產生的連接放到列表中class SocketServer(threading.Thread): def __init__(self, socket_list): super().__init__() self.socket_list = socket_list def run(self): sock = socket.socket() sock.bind(('', 9090)) sock.listen(5) print("Start listen...") while True: conn, addr = sock.accept() print("Connect by", addr) self.socket_list.append((conn, addr))# 線程3:從隊列中取出數字,遍歷連接列表,發送到所有客戶端class Printer(threading.Thread): def __init__(self, work_queue, socket_list): super().__init__() # 必須調用 self.work_queue = work_queue self.socket_list = socket_list def run(self): while True: num = self.work_queue.get() # 當隊列為空時,會阻塞,直到有數據 for sock, addr in self.socket_list: # 遍歷保存連接的列表 print("Send", num, "To", addr) try: sock.sendall(bytes(num + '/r/n', 'utf-8')) # 把字符串轉換成字節數組發送 except: print("Disconnect by", addr) # 如果連接斷開,發送會失敗 self.socket_list.remove((sock, addr)) # 從列表中刪除斷開的連接def main(): work_queue = queue.Queue() socket_list = [] # 為了更安全可靠,從多線程訪問列表時應該加鎖, # 這里做了簡化,因為列表的增加刪除操作基本上可以認為是線程安全的 socket_server = SocketServer(socket_list) socket_server.daemon = True socket_server.start() printer = Printer(work_queue, socket_list) printer.daemon = True # 當主線程退出時子線程也退出 printer.start() producer = Producer(work_queue) producer.daemon = True # 當主線程退出時子線程也退出 producer.start() time.sleep(1) # 這里要暫停一下,否則執行下一條語句時,會因隊列為空而直接返回 work_queue.join() # 主線程會停在這里,直到所有數字被get(),并且task_done(),因為沒有調用task_done(),所在這里會一直阻塞,直到用戶按^Cif __name__ == '__main__': main()https://docs.python.org/3.1/library/asynchat.html
https://docs.python.org/3.1/library/asyncore.html
https://docs.python.org/3/library/socket.html
https://docs.python.org/3.2/library/socketserver.html
http://stackoverflow.com/questions/6319207/are-lists-thread-safe
新聞熱點
疑難解答