阻塞IO与非阻塞IO

阻塞IO:操作系统等数据,只发生了一次调用,最大问题:cpu被耽搁了

非阻塞IO:问题:1、发生多次系统调用     2、数据不及时

IO multiplexing model io多路复用,多一个系统调用,但可以建立多链接

#########################################blocking IO
# import socket
#
# sk=socket.socket()
#
# sk.bind(("127.0.0.1",8080))
#
# sk.listen(5)
#
# while 1:
#     conn,addr=sk.accept()
#
#     while 1:
#         conn.send("hello client".encode("utf8"))
#         data=conn.recv(1024)
#         print(data.decode("utf8"))

########################################################nonblocking IO

# import time
# import socket
# sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# sk.bind(('127.0.0.1',6667))
# sk.listen(5)
# sk.setblocking(False)
# print ('waiting client connection .......')
# while True:
#     try:
#
#         connection,address = sk.accept()   # 进程主动轮询
#         print("+++",address)
#         client_messge = connection.recv(1024)
#         print(str(client_messge,'utf8'))
#         connection.close()
#     except Exception as e:
#         print (e)
#         time.sleep(4)

########################################################io多路复用

# import socket
# import select
# sk=socket.socket()
# sk.bind(("127.0.0.1",9904))
# sk.listen(5)
# inp=[sk,]
# while True:
#
#     r,w,e=select.select(inp,[],[],5) #[sk,conn]
#
#     for i in r:#[sk,]
#         conn,add=i.accept()
#         print(conn)
#         print("hello")
#         inp.append(conn)
#     print('>>>>>>')
View Code

猜你喜欢

转载自www.cnblogs.com/jintian/p/11026780.html