Python---io multiplexing

Source: Internet
Author: User

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 data
View 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.