您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在python怎么中實現socket服務端的并發,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
服務端:多進程和多線程的開啟方式相同。
缺點:<1> 由于Cpython的GIL,導致同一時間無法運行多個線程;<2> 不可能無限開進進程或線程
解決辦法:多進程、concurrent.futures.ProcessPoolExecutor、線程池
import socket from multiprocessing import Process from threading import Thread class MyTcpServer: def __init__(self, ip, port): self.ip = ip self.port = port self.server = socket.socket() self.server.bind((self.ip, self.port)) self.server.listen(5) def wait_accept(self): conn, addr = self.server.accept() return conn, addr def handle_request(self, conn): while 1: try: data = conn.recv(1024) if not data: break conn.send(data.upper()) except Exception as e: print(e) break conn.close() if __name__ == '__main__': server = MyTcpServer('127.0.0.1', 8888) while 1: conn, addr = server.wait_accept() p = Process(target=server.handle_request, args=(conn, )) # 創建一個進程 p.start() # 告訴操作提供,開啟這個進程
異步提交任務,支持異步接收返回結果(submit返回一個futures對象,調用add_done_callback方法)
import socket from concurrent.futures import ProcessPoolExecutor # from concurrent.futures import ThreadPoolExecutor class MyTcpServer: def __init__(self, ip, port): self.ip = ip self.port = port self.server = socket.socket() self.server.bind((self.ip, self.port)) self.server.listen(5) def wait_accept(self): conn, addr = self.server.accept() return conn, addr def handle_request(self, conn): while 1: try: data = conn.recv(1024) if not data: break conn.send(data.upper()) except Exception as e: print(e) break conn.close() if __name__ == '__main__': server = MyTcpServer('127.0.0.1', 8888) pool = ProcessPoolExecutor(5) # 5個進程一直服務 while 1: conn, addr = server.wait_accept() pool.submit(server.handle_request, conn) # 異步提交任務
優點:簡化socket服務端創建流程。
提供服務端串行和并發兩種服務模式(TCPServer,ThreadingTCPServer)
缺點:windows上無法使用多進程實現并發
import socketserver class MyTcpHandler(socketserver.BaseRequestHandler): def handle(self): # 通信循環 while 1: try: data = self.request.recv(1024) if not data: break self.request.send(data.upper()) except Exception as e: print(e) break self.request.close() if __name__ == '__main__': ip_port = '127.0.0.1', 8888 server = socketserver.ThreadingTCPServer(ip_port, MyTcpHandler) # 異步處理 server.serve_forever() # 連接循環
協程
優點:單線程內實現并發,代碼級別模擬IO切換,提高程序運行效率
from gevent import spawn, monkey;monkey.patch_all() # 猴子補丁,補丁:常規IO import socket class MyTcpServer: def __init__(self, ip, port, my_spawn): self.ip = ip self.port = port self.server = socket.socket() self.server.bind((self.ip, self.port)) self.server.listen(5) self.spawn = my_spawn # 保存spawn本地 def wait_accept(self): while 1: conn, addr = self.server.accept() self.spawn(self.handle_request, conn) # 檢測 handle_request的io def handle_request(self, conn): while 1: try: data = conn.recv(1024) if not data: break conn.send(data.upper()) except Exception as e: print(e) break conn.close() if __name__ == '__main__': server = MyTcpServer('127.0.0.1', 8888, spawn) g1 = server.spawn(server.wait_accept) # 檢測wait_accept的io g1.join() # 等待g1運行結束,即一直在循環檢測io
上述就是小編為大家分享的怎么在python怎么中實現socket服務端的并發了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。