selectors实现高并发
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selectors实现高并发相关的知识,希望对你有一定的参考价值。
1. 下面的例子,客户端给服务端发送消息,服务端把消息返回
server
#!/usr/bin/env python import selectors import socket import time import threading sel = selectors.DefaultSelector() def echo(conn,cmd,filename): time.sleep(3) conn.send((cmd + ‘ ‘ + filename).encode()) def accept_client(sock,mask): conn,addr = sock.accept() print(‘conn:{},addr:{},mask:{}‘.format(conn,addr,mask)) # conn.setblocking(False) sel.register(conn, selectors.EVENT_READ, read_client) def read_client(conn,mask): data = conn.recv(1024).decode().strip() if data: if len(data.split(‘ ‘)) == 2: cmd = data.split(‘ ‘)[0] filename = data.split(‘ ‘)[1] t = threading.Thread(target=echo,args=(conn,cmd,filename)) #这里使用了线程,不然程序会变成串行的,执行一个客户端任务,sleep(3)然后再执行下一个客户端任务... t.start() # time.sleep(1) # conn.send((cmd + ‘ ‘ + filename).encode()) else: conn.send((‘usage: put filename ;get filename‘).encode()) else: print(‘{} closed‘.format(conn)) sel.unregister(conn) conn.close() server = socket.socket() server.bind((‘localhost‘,1235)) server.listen(12340) # server.setblocking(False) sel.register(server, selectors.EVENT_READ,accept_client) while True: events = sel.select() for key,mask in events: callable = key.data callable(key.fileobj,mask)
client
#!/usr/bin/env python import socket import threading import time ip_port = (‘localhost‘,1235) socks = [socket.socket() for i in range(1000)] m = b‘get abc‘ time_list = [] def run(s): s.connect(ip_port) s.send(m) data = s.recv(1024) print(s,data) start_time = time.time() for s in socks: t = threading.Thread(target=run,args=(s,)) t.start() time_list.append(t) for i in time_list: i.join() print(‘time:{}‘.format(start_time - time.time()))
2. 实现并发put和get文件
以上是关于selectors实现高并发的主要内容,如果未能解决你的问题,请参考以下文章
面试难题:Netty如何解决Selector空轮询BUG?(图解+秒懂+史上最全)