190707select和selector模块

 公司新闻     |      2019-09-27 01:14
readable, writeable, exceptional = select.select print for r in readable: if r is server: conn, addr = server.accept print inputs.append msg_dic[conn] = queue.Queue else: data = r.recv print msg_dic[r].put outputs.append for w in writeable: data_to_clinet =msg_dic[w].get w.send outputs.remove for e in exceptional: if e in outputs: outputs.remove inputs.remove del msg_dic[e] 二、selector模块
# Author:Li Dongfei
import selectors, socket
sel = selectors.DefaultSelector
def accept:
 conn, addr = sock.accept
 print
 conn.setblocking
 sel.register
def read:
 data = conn.recv
 if data:
 print, 'to', conn)
 conn.send
 else:
 print
 sel.unregister
 conn.close
sock = socket.socket
sock.bind)
sock.listen
sock.setblocking
sel.register
while True:
 events = sel.select
 for key, mask in events:
 callback = key.data
 callback
猜你喜欢