Python Development Foundation---IO model

Source: Internet
Author: User
Tags epoll

IO Model Classification

Five IO Model

Blocking IO blocking IO

nonblocking io non-blocking IO

IO multiplexing io multiplexing

Signal driven IO signal driver IO

Asynchronous IO Asynchronous IO

Signal driven IO (signal driven IO) is not commonly used in practice, so there are only four Io Model left.

Two processes for network IO

For a network IO, two system objects are involved, one is the process (or thread) that calls the IO, and the other is the system kernel (kernel). When a read operation occurs, it goes through two stages:

    • Waiting for data preparation (waiting for the data to being ready): Waiting for the system to receive
    • Copying data from the kernel into the process (Copying the data from the kernel to the process): processes get the information from the system cache

Synchronous IO: There are blocking states at any stage during these two processes.

Blocking IO, non-blocking IO, IO multiplexing are all synchronous IO

Asynchronous IO: Full non-blocking IO

Asynchronous IO is asynchronous IO (really no problem)

Blocking io (Blocking io)

UDP packet: When the user process calls Recvfrom this system call, kernel begins the first phase of IO: Preparing the data. For network IO, there are times when the data has not arrived at the beginning (for example, a full UDP packet has not been received), and kernel waits for enough data to arrive. On this side of the user process, the entire process is blocked. When kernel waits until the data is ready, it copies the data from the kernel to the user's memory, and then kernel returns the result, and the user process removes the block state and re-runs it.

The blocking Io is characterized by block in both phases of IO execution.

Example:

1 #服务端 2 Import Socket 3 Sock=socket.socket ()   #默认是TCP 4 Sock.bind (("127.0.0.1", 8088)) 5  6 Sock.listen (5) 7 while T Rue:8     conn,addr=sock.accept ()   #默认是就是阻塞的方式, listener waiting for client connection (phase One): Blocking in wait 9                               #客户端连接后接收数据 (phase II): Socket object and client address, Although the process of receiving data is fast but is actually blocking the     true:11         data=conn.recv (1024x768)    #也是两个阶段的阻塞12         print (Data.decode (' UTF8 ' )         if Data.decode (' utf8 ') = = ' Q ':             break15         respnse=input (' >>>> ')         conn.send ( Respnse.encode (' UTF8 ')) #客户端20 import socket21 sock=socket.socket () sock.connect (("127.0.0.1", 8088)) 23 24 While true:25     data=input (' >>> '). Strip ()     sock.send (Data.encode (' UTF8 '))     S_data = SOCK.RECV (1024x768)    #两个阶段的阻塞28     print (S_data.decode (' UTF8 '))

Non-blocking IO (non-blocking io)

When the user process issues a read operation, if the data in the kernel is not yet ready, it does not block the user process, but returns an error immediately. So the user process does not need to wait, but immediately get a result, the user process to determine the result is an error, it will know that the data is not ready, so it can send the read operation again. Once the data in the kernel is ready and again receives the system call of the user process, it immediately copies the data to the user's memory and then returns. In this process, the user process is required to constantly proactively ask kernel data for good.

Non-blocking is actually a large block of time blocking into a small number of more than N, each time Recvform system calls between, can do something else, and then initiate the Recvform system call, repeat the process is often called polling. Poll the kernel data until the data is ready, and then copy the data to the process for data processing. It is important to note that the process of copying data is still a blocking state.

Pros: Ability to do other work while waiting for the task to complete (including submitting other tasks, i.e. "backstage" can have multiple tasks at the same time).

Disadvantage: The response delay for task completion is increased because each time a read operation is polled, the task may be completed at any time between polling two times. This can result in a decrease in overall data throughput.

 1 #服务端 2 3 Import Socket 4 Import time 5 sock=socket.socket (socket.af_inet,socket. SOCK_STREAM) #默认是TCP 6 Sock.bind (("127.0.0.1", 8088)) 7 Sock.listen (5) 8 sock.setblocking (False) 9 while True:11 t             Ry:12 print (' Server Waiting ') conn, addr = sock.accept () # Default is a blocking way, waiting for the client to connect to a while true:15  data = CONN.RECV (1024x768) #这边也是阻塞的IO16 print (Data.decode (' UTF8 ')) if Data.decode (' UTF8 ') = = ' Q ': break19 respnse = input (' >>>> ') conn.send (Respnse.encode ( ' UTF8 ') except Exception as e:22 print (e) time.sleep (4) #客户端26 import Socket27 sock=socket. Socket (Socket.af_inet,socket. SOCK_STREAM) #默认是TCP28 while true:30 Sock.connect (("127.0.0.1", 8088)) #因为服务端recv也是非阻塞, so keep reconnecting data=inp UT (' >>> '). Strip () Sock.send (Data.encode (' UTF8 ')) S_data = Sock.recv (1024x768) print (S_data.decode (' UTF8 ')) 

io multiplexing (io multiplexing)

Io multiplexing, also known as event driven IO, is implemented in the following way: Select,poll or Epoll

The benefit of IO multiplexing is that a single process can handle multiple network-connected IO simultaneously

When the user process invokes select, the entire process is blocked, and at the same time, kernel "monitors" all the select-responsible sockets, and when the data in any one socket is ready, select returns. This time the user process then invokes the read operation, copying the data from the kernel to the user process. There are two times in this process when the system call, select blocking time and Recvfrom blocking. The advantage of multiplexing is that it can handle large quantities of connection at the same time, not a single or small amount, or less than multi-threading + blocking IO.

Select Example:

 1 #服务端 2 Import Socket 3 Import Select 4 Sock=socket.socket () 5 Sock.bind (("127.0.0.1", 8088) 6 Sock.listen (5) 7 8 inp=[ Sock,] #定义监听的套接字对象列表, list tables can have multiple objects 9 while true:11 #字段顺序: input list, output list, Error list, date (can not be written) R=selec T.select (Inp,[],[],none) #对比的是sock. Accept (), this step only to do the listening thing, listen to which socket object activity, when there is no client connection time will block 13 # when the monitor heard the active socket object,  return value to R14 print (' R ', R) (' R ', r[0])-#r接收的返回是一个元组, r[0] is the active list of objects for obj in r[0]:19 if obj = = Sock: #如果活动的对象是sock, then the client object is added to the listener list, the client then sends the data, the client's object activity is triggered conn,addr=obj.accept () #accept只做第二个阶段的事情, retrieve the data: C Lient socket object and address print (CONN,ADDR) inp.append (conn) else:24 Data=obj.rec V (1024x768) print (Data.decode (' UTF8 ')) resp=input (' >>> ') obj.send (Resp.enco De (' UTF8 ')) #客户端30 import socket31 sock=socket.socket () sock.connect (("127.0.0.1", 8088)) and true:34 dat A=input (' >>> '). Strip () 35    Sock.send (Data.encode (' UTF8 ')) S_data = Sock.recv (1024x768) PNS print (S_data.decode (' UTF8 ')) 

Because a For loop is used, when multiple clients send messages to the server, they can only be processed sequentially.

Multiplexing is only possible with select under Windows

In Linux can be implemented using SELECT, poll, Epoll, the recommended use of epoll, contrast:

Select and poll monitoring mode for polling, that is, each time to cycle through the listener list, inefficient, and select has a connection limit, poll unlimited

Epoll connection number is infinite, the difference is that the listening mode is different, each socket object binds a callback function, when the socket object is active, triggers a callback function, writes itself to the activity list, Epoll directly invokes the activity list

Signal-driven IO (signal driven IO)

Not used, no instructions.

Asynchronous IO (asynchronous I/O)

After the user process initiates the read operation, you can begin to do other things immediately. On the other hand, from the perspective of kernel, when it receives a asynchronous read, first it returns immediately, so no block is generated for the user process. Then, kernel waits for the data to be ready and then copies the data to the user's memory, and when all this is done, kernel sends a signal to the user process to tell it that the read operation is complete.

IO Model Differences

Selectors module

The module can automatically select multiplexing mode according to the system platform.

 1 #服务端 2 Import selectors 3 import socket 4 5 sel=selectors. Defaultselector () 6 7 def accept (Sock,mask): 8 conn,addr=sock.accept () #4, get Client conn object and address 9 print (' accetped ', Conn, ' from ', addr) conn.setblocking (False) sel.register (conn,selectors. Event_read,read) #5, registers the Conn object, binds the Conn object and the function read to the DEF Read (Conn,mask): Data=conn.recv (1024x768) #9, the server receives the cancellation via the Conn object         Data:16 print (' Echoing ', repr (data), ' to ', conn) conn.send (data) Print (' closing ', conn) sel.unregister (conn) conn.close () sock=socket.socket (' 127.0 .0.1 ', 8088)) (Sock.listen) sock.setblocking (False) sel.register (sock,selectors. event_read,accept) #sock对象注册绑定accept函数28 while True:30 #不管是哪个方式, are the socket objects that use the Select method to listen for activity events=sel.select ( ) #1, perform sel blocking monitoring, when there is a client connection, activates the sock object, returns a list of information about the sock object that holds the activity, and the client sends a message via Conn object, activating con in the SEL listener list. N object that returns a list of information about the activity Conn Object PrinT (Events,type (events)) Key,mask in events:35 print (mask) print (key.data) #socket对象注册绑定的accep t function PNs Print (key.fileobj) Callback=key.data #2, obtains the returned sock bound function the #7, obtains the return conn The bound function, callback (Key.fileobj,mask) #3, Key.fileobj is the sock object, executes the function #8, executes the function read, and Incoming Conn Object #客户端45 import socket46 sock=socket.socket () sock.connect (("127.0.0.1", 8088)) and true:49 Data= Input (' >>> '). Strip () Sock.send (Data.encode (' UTF8 ')) S_data = Sock.recv (1024x768) print (s_data.decod E (' UTF8 '))

Python Development Foundation---IO model

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.