您好,登录后才能下订单哦!
这期内容当中小编将会给大家带来有关怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。