The IO here refers to the network IO
IO multiplexing via select module in Python, select, poll, Epoll, etc. in select module
The following example implements IO multiplexing with the Select module
Only IO multiplexing can only implement pseudo concurrency
Server-side
#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'ImportSocketImportSelectsk=socket.socket () Sk.bind ('127.0.0.1', 9999,)) Sk.listen (5) Inputs=[sk,]#objects to listen to whileTrue:#The rlist element is the socket object #listen to the SK (server side) object, if the SK object changes, indicating that there is a client to connect, at this time the rlist value is [SK] #listen to the Conn object, if the conn changes, indicating that the client has a new message sent over, at this time the value of rlist is [client]Rlist,w,e=select.select (inputs,[],[],1) #print (rlist) Print(len (inputs), Len (rlist)) forRinchRlist#If the Rlist list is empty, this for loop does not execute ifR==sk:#indicates that there is a new client to connect #print (R)Conn,addr=r.accept ()#Create a Conn object for a new clientInputs.append (conn)#Place the new Conn object in the inputsConn.sendall (Bytes ('Hello', encoding='Utf-8')) Else: R.recv (1024)#indicates that the client sent the dataView Code
Client
#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'ImportSocketImportSelectsk=socket.socket () Sk.connect ('127.0.0.1', 9999,)) Data=SK.RECV (1024)Print(data) whileTRUE:INP=input ('>>>') Sk.sendall (bytes (inp,encoding='Utf-8') ) Sk.close ()View Code
Python---io multiplexing