Python network programming: IO multiplexing

Source: Internet
Author: User
Tags epoll

IO multiplexing: Can listen to multiple file descriptors (socket objects) (file handle), once the file handle changes, you can sense.

1Sk1 = Socket.socket ()2Sk1.bind (' 127.0.0.1 ', 8001)3Sk1.listen ()4 5# sk2 = Socket.socket ()6# Sk2.bind ((' 127.0.0.1 ', 8002))7# Sk2.listen ()8While True:9Conn,address = Sk.accept () #阻塞等待客户端连接, connection, client address informationTen     Print(conn,address) OneConn.sendall (Bytes (' Beijing welcomes you ', encoding= ' utf-8 ')) AWhile True: -Ret_bytes = CONN.RECV (1024) -Ret_str = str (ret_bytes,encoding= ' utf-8 ') the         ifRet_str = = ' Q ': -Break -Conn.sendall (bytes (ret_str+ ' good ', encoding= ' utf-8 ')) -     Print(Address,conn) + 

Sk1\sk2 is called a file descriptor, a file handle. The above program can only perform sk1.

IO multiplexing has a mechanism that can accept multiple file descriptors, and once anyone has changed, deal with them.

1 ImportSocket2Sk1 = Socket.socket ()3Sk1.bind (' 127.0.0.1 ', 8001)4Sk1.listen ()5 6Sk2 = Socket.socket ()7Sk2.bind (' 127.0.0.1 ', 8002)8Sk2.listen ()9 TenSK3 = Socket.socket () OneSk3.bind (' 127.0.0.1 ', 8002) ASk3.listen () -  -inputs = [Sk1,sk2] the#本例是用select伪装成多处理用户连接请求, the advantage of a socket is that you don't have to wait? The difficulty lies in the inputs two types of sockets, the client socket and the server socket? - ImportSelect -While True: -#[sk1,sk2,],select internal automatic monitoring Sk1,sk2,sk3 three objects, once a handle has changed, it will be placed in the r_list. The first change is sk.accept (), that is, someone comes to sk1. r_list = [Sk1] +R_list,w_list,e_list = Select.select (inputs,outputs,inputs,1) #等一秒看是否有人来连接, the next loop is not executed. The maximum time to wait. -     Print(' Listening socket object%d '% len (inputs)) +     Print(r_list) AFor Sk1_or_conn in R_list: at 

IO multiplexing is the function that the operating system provides, and we just use Python to call it, in three ways, Select,poll,epoll. Windows only supports Select.

Select the underlying implementation principle:

The system internal C language for the For loop detection, when the file handle send changes to tell us. Performance is low, and only up to 1024 file descriptors are supported.


So then there is the poll, there is no limit to the number of file descriptors, but the bottom layer is also implemented with a for loop.

Then there is the Epoll, the bottom is not used for the loop, but asynchronous implementation, the handle is put in, who has changed who actively told Epoll, rather than for the loop over and over again monitoring. So the performance of Epoll is the highest.

The inside of Nginx is socket combined with Epoll to monitor user request.

For SK in E_list: #e_list是发生错误的文件描述符列表

Inputs.remove (SK)


1 ImportSocket2Sk1 = Socket.socket ()3Sk1.bind (' 127.0.0.1 ', 8001)4Sk1.listen ()5 6# sk2 = Socket.socket ()7# Sk2.bind ((' 127.0.0.1 ', 8002))8# Sk2.listen ()9#Ten# SK3 = Socket.socket () One# Sk3.bind ((' 127.0.0.1 ', 8003)) A# Sk3.listen () -inputs = [SK1] -outputs = [] the#本例是用select伪装成多处理用户连接请求, the advantage of a socket is that you don't have to wait? The difficulty lies in the two types of sockets in inputs, the client socket and the server socket. - ImportSelect -While True: -#[sk1,sk2,],select internal automatic monitoring Sk1,sk2,sk3 three objects, once a handle has changed +R_list,w_list,e_list = Select.select (inputs,outputs,inputs,1) -     Print(' Listening socket object%d '% len (inputs)) +     Print(r_list) AFor Sk1_or_conn in R_list: at#每一个连接对象 -         ifSk1_or_conn = = Sk1: -#表示有新用户来连接 -Conn, address = Sk1_or_conn.accept () -Inputs.append (conn) -         Else: in#有老用户发消息了 -Try toData_bytes = SK1_OR_CONN.RECV (1024) +Except Exception as ex: -#如果用户中断连接 theInputs.remove (Sk1_or_conn) *             Else: $#用户正常发消息Panax Notoginseng# data_str = str (data_bytes,encoding= ' utf-8 ') -# Sk1_or_conn.sendall (bytes (data_str+ ' good ', encoding = ' utf-8 ')) theOutputs.append (Sk1_or_conn) +#w_list仅仅存谁给我发过消息, this parameter is used if you want to read and write the separation. AFor Conn in w_list: theConn.sendall (bytes (' Hello ', encoding= ' utf-8 ')) +OUTPUTS.REMOVE (conn) - 
IO multiplexing processing multi-user requests

The difficulty is to understand that r_list is not equal to inputs.

Inputs inside two types of data, one is the server socket SK1, the other is the client socket (as long as someone to connect to apend a socket object)

And r_list inside is the change of the object, a number of users to even Sk1,r_list is sk1, and there are users send messages, that r_list into the message of the socket object.


Socketserver:
Select/epoll + socket + multithreading for concurrent operations.

Python network programming: IO multiplexing

Related Article

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.