程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mastering the multiplexing IO model in the learning notes of Python introductory development

編輯:Python

The point of this section

  • Mastering multiplexing IO Model

The duration of this section needs to be controlled 15 Within minutes

Multiplexing IO(IO multiplexing)

IO multiplexing The word may be a little strange , But if I say select/epoll, I think we can all understand . Some places also call this IO The way is Event driven IO

(event driven IO). We all know ,select/epoll The good thing about it is that it's a single process You can handle multiple network connections at the same time IO. Its basic principle is select/epoll This function Will constantly poll all the socket, When a socket There's data coming in , Just inform the user of the process . Its flow chart is as follows :

 When the user process calls select, Then the whole process will be block, At the same time ,kernel Meeting “ monitor ” all select conscientious socket,
When any one socket The data in is ready ,select It will return . At this time, the user process calls read operation , Take data from kernel Copy to user process .
This figure and blocking IO It's not that different , In fact, it's worse . Because here we need to use two system calls \(select and recvfrom\),
and blocking IO Only one system call was called \(recvfrom\). however , use select The advantage is that it can handle multiple connection.

emphasize :

1. If the number of connections processed is not very high , Use select/epoll Of web server It's not necessarily better than using multi-threading + blocking IO Of web server Better performance , Maybe the delay is even greater .select/epoll The advantage is not that you can handle a single connection faster , It's about being able to handle more connections .

2. In the multiplexing model , For each of these socket, It's usually set to non-blocking, however , As shown in the figure above , For the entire user process In fact, it has been block Of . It's just process Be being select This function block, Rather than being socket IO to block.

Conclusion : select The advantage is that it can handle multiple connections , Not for single connection

select The Internet IO Model example

# Server side
from socket import *
import select
server = socket(AF_INET, SOCK_STREAM)
server.bind(('127.0.0.1',8093))
server.listen(5)
server.setblocking(False)
print('starting...')
rlist=[server,]
wlist=[]
wdata={}
while True:
rl,wl,xl=select.select(rlist,wlist,[],0.5)
print(wl)
for sock in rl:
if sock == server:
conn,addr=sock.accept()
rlist.append(conn)
else:
try:
data=sock.recv(1024)
if not data:
sock.close()
rlist.remove(sock)
continue
wlist.append(sock)
wdata[sock]=data.upper()
except Exception:
sock.close()
rlist.remove(sock)
for sock in wl:
sock.send(wdata[sock])
wlist.remove(sock)
wdata.pop(sock)
# client
from socket import *
c=socket(AF_INET,SOCK_STREAM)
c.connect(('127.0.0.1',8081))
while True:
msg=input('>>: ')
if not msg:continue
c.send(msg.encode('utf-8'))
data=c.recv(1024)
print(data.decode('utf-8'))

select monitor fd Analysis of the process of change :

 User process creation socket object , Copy monitor fd To kernel space , every last fd It will correspond to a system file table , Kernel space fd After responding to the data ,
It will send a signal to the user that the process data has arrived ;
The user process sends the system call , such as (accept) Put the kernel space data copy To user space , At the same time, it is used as the data cleaning of the kernel space of the receiving data terminal ,
When you re monitor like this fd There are new data that can respond to ( The sender is based on TCP The protocol needs to be cleared after receiving the response ).

The advantages of this model :

 Compared with other models , Use select() The event driven model of is single threaded ( process ) perform , Less occupied resources , It doesn't cost much CPU, At the same time, it can provide services for multiple clients .
If you try to build a simple event driven server program , This model has certain reference value .

The disadvantages of this model :

 First select() An interface is not an implementation “ Event driven ” The best choice . Because when the handle value to be detected is large ,select() The interface itself takes a lot of time to poll the handles .
Many operating systems provide more efficient interfaces , Such as linux Provides epoll,BSD Provides kqueue,Solaris Provides /dev/poll,….
If you need to implement more efficient server programs , similar epoll Such an interface is more recommended . Unfortunately, different operating systems are special for epoll The interface is very different ,
So use something like epoll It is difficult to implement a server with better cross platform capability .
secondly , The model combines event detection with event response , Once the execution body of event response is huge , It's catastrophic for the whole model .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved