python选择器selectors
1 import selectors # 常用于非阻塞的socket的编程中 2 import threading 3 import socket 4 import logging 5 6 FORMAT = '%(asctime)s %(threadName)s %(thread)d %(message)s' # 日志格式 7 logging.basicConfig(format=FORMAT,level=logging.INFO) 8 9 def accept(sock:socket.socket,mask): # 接受 10 conn,raddr = sock.accept() 11 conn.setblocking(False) 12 key = selector.register(conn,selectors.EVENT_READ,read) 13 logging.info(key) 14 15 def read(conn:socket.socket,mask): # 读数据 16 data = conn.recv(1024) 17 msg = 'msg is {}'.format(data.decode()) 18 conn.send(msg.encode()) 19 20 selector = selectors.DefaultSelector() # 选择器 选择最佳实现,是当前平台上可用的最高效实现的别名 21 sock = socket.socket() 22 sock.bind(('127.0.0.1',8080)) 23 sock.listen() 24 logging.info(sock) 25 sock.setblocking(False) # 设置非阻塞 26 key = selector.register(sock,selectors.EVENT_READ,accept) # 注册到选择器 27 logging.info(key) 28 29 e = threading.Event() # 类实现事件对象。 30 31 def select(e): 32 while not e.is_set(): 33 34 events = selector.select() 35 36 print('========') 37 38 for key,mask in events: 39 40 logging.info(key) 41 42 logging.info(mask) 43 44 callback = key.data 45 46 callback(key.fileobj,mask) 47 threading.Thread(target=select,args=(e,),name='select').start() 48 49 def main(): 50 while not e.is_set(): 51 cmd = input('>>>') 52 print("1111111") 53 if cmd.strip() == 'quit': 54 e.set() 55 fobjs = [] 56 57 logging.info('{}'.format(list(selector.get_map().items()))) 58 for fd ,key in selector.get_map().items(): 59 print(fd) 60 print(key) 61 fobjs.append(key.fileobj) 62 for fobj in fobjs: 63 64 selector.unregister(fobj) 65 66 fobj.close() 67 selector.close() 68 else: 69 print("22") 70 71 logging.info('{}'.format(list(selector.get_map().items()))) 72 73 if __name__ == '__main__': 74 main()
import selectors # 常用于非阻塞的socket的编程中
import threading
import socket
import logging
FORMAT = '%(asctime)s %(threadName)s %(thread)d %(message)s' # 日志格式
logging.basicConfig(format=FORMAT,level=logging.INFO)
def accept(sock:socket.socket,mask): # 接受
conn,raddr = sock.accept()
conn.setblocking(False)
key = selector.register(conn,selectors.EVENT_READ,read)
logging.info(key)
def read(conn:socket.socket,mask): # 读数据
data = conn.recv(1024)
msg = 'msg is {}'.format(data.decode())
conn.send(msg.encode())
selector = selectors.DefaultSelector() # 选择器 选择最佳实现,是当前平台上可用的最高效实现的别名
sock = socket.socket()
sock.bind(('127.0.0.1',8080))
sock.listen()
logging.info(sock)
sock.setblocking(False) # 设置非阻塞
key = selector.register(sock,selectors.EVENT_READ,accept) # 注册到选择器
logging.info(key)
e = threading.Event() # 类实现事件对象。
def select(e):
while not e.is_set():
events = selector.select()
print('========')
for key,mask in events:
logging.info(key)
logging.info(mask)
callback = key.data
callback(key.fileobj,mask)
threading.Thread(target=select,args=(e,),name='select').start()
def main():
while not e.is_set():
cmd = input('>>>')
print("1111111")
if cmd.strip() == 'quit':
e.set()
fobjs = []
logging.info('{}'.format(list(selector.get_map().items())))
for fd ,key in selector.get_map().items():
print(fd)
print(key)
fobjs.append(key.fileobj)
for fobj in fobjs:
selector.unregister(fobj)
fobj.close()
selector.close()
else:
print("22")
logging.info('{}'.format(list(selector.get_map().items())))
if __name__ == '__main__':
main()