您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在python怎么中實現(xiàn)socket服務(wù)端的并發(fā),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
服務(wù)端:多進程和多線程的開啟方式相同。
缺點:<1> 由于Cpython的GIL,導(dǎo)致同一時間無法運行多個線程;<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, )) # 創(chuàng)建一個進程 p.start() # 告訴操作提供,開啟這個進程
異步提交任務(wù),支持異步接收返回結(jié)果(submit返回一個futures對象,調(diào)用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個進程一直服務(wù) while 1: conn, addr = server.wait_accept() pool.submit(server.handle_request, conn) # 異步提交任務(wù)
優(yōu)點:簡化socket服務(wù)端創(chuàng)建流程。
提供服務(wù)端串行和并發(fā)兩種服務(wù)模式(TCPServer,ThreadingTCPServer)
缺點:windows上無法使用多進程實現(xiàn)并發(fā)
import socketserver class MyTcpHandler(socketserver.BaseRequestHandler): def handle(self): # 通信循環(huán) 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() # 連接循環(huán)
協(xié)程
優(yōu)點:單線程內(nèi)實現(xiàn)并發(fā),代碼級別模擬IO切換,提高程序運行效率
from gevent import spawn, monkey;monkey.patch_all() # 猴子補丁,補丁:常規(guī)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運行結(jié)束,即一直在循環(huán)檢測io
上述就是小編為大家分享的怎么在python怎么中實現(xiàn)socket服務(wù)端的并發(fā)了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。