IO多路复用

Posted lvxw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO多路复用相关的知识,希望对你有一定的参考价值。

I/O多路复用(multiplexing)的本质是通过一种机制(系统内核缓冲I/O数据),让单个进程可以监视多个文件描述符,一旦某个描述符就绪(一般是读就绪或写就绪),能够通知程序进行相应的读写操作。

select、poll 和 epoll 都提供的 IO 复用方式。

(1)select,poll实现需要自己不断轮询所有fd集合,直到设备就绪,期间可能要睡眠和唤醒多次交替。而epoll其实也需要调用 epoll_wait不断轮询就绪链表,期间也可能多次睡眠和唤醒交替,但是它是设备就绪时,调用回调函数,把就绪fd放入就绪链表中,并唤醒在 epoll_wait中进入睡眠的进程。虽然都要睡眠和交替,但是select和poll在“醒着”的时候要遍历整个fd集合,而epoll在“醒着”的 时候只要判断一下就绪链表是否为空就行了,这节省了大量的CPU时间,这就是回调机制带来的性能提升。

(2)select,poll每次调用都要把fd集合从用户态往内核态拷贝一次,并且要把current往设备等待队列中挂一次,而epoll只要 一次拷贝,而且把current往等待队列上挂也只挂一次(在epoll_wait的开始,注意这里的等待队列并不是设备等待队列,只是一个epoll内 部定义的等待队列),这也能节省不少的开销。
selectors实现IO多路复用

#服务端
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import select, socket

response = b"hello world"

#创建一个server socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind((localhost, 8080))
serversocket.listen(1)
serversocket.setblocking(0)

inputs = [serversocket, ]

while True:
    rlist, wlist, xlist = select.select(inputs, [], [])
    for sock in rlist:
        # server socket读就绪
        if sock == serversocket:
            con, addr = serversocket.accept()
            #将这个connection添加到读就绪中
            inputs.append(con)
        else:
            data = sock.recv(1024)
            if data:
                sock.send(response)
                #从读就绪的list中删除
                inputs.remove(sock)
                sock.close()
#客户端
from socket import *
c=socket(AF_INET,SOCK_STREAM)
c.connect((127.0.0.1,8080))

while True:
    msg=input(>>: )
    if not msg:continue
    c.send(msg.encode(utf-8))
    data=c.recv(1024)
    print(data.decode(utf-8))

poll实现IO多路复用

server端:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import select, socket

response = b"hello world"

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind((‘192.168.199.197‘, 8080))
serversocket.listen(1)
serversocket.setblocking(0)

#
poll = select.poll()
poll.register(serversocket.fileno(), select.POLLIN)

connections = 
while True:
    for fd, event in poll.poll():
        if event == select.POLLIN:
            if fd == serversocket.fileno():
                con, addr = serversocket.accept()
                poll.register(con.fileno(), select.POLLIN)
                connections[con.fileno()] = con
            else:
                con = connections[fd]
                data = con.recv(1024)
                if data:
                    poll.modify(con.fileno(), select.POLLOUT)
        elif event == select.POLLOUT:
            con = connections[fd]
            con.send(response)
            poll.unregister(con.fileno())
            con.close()

 epoll实现IO多路复用

import socket, select

EOL1 = b\\n\\n
EOL2 = b\\n\\r\\n
response  = bHTTP/1.0 200 OK\\r\\nDate: Mon, 1 Jan 1996 01:01:01 GMT\\r\\n
response += bContent-Type: text/plain\\r\\nContent-Length: 13\\r\\n\\r\\n
response += bHello, world!

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind((0.0.0.0, 8080))
serversocket.listen(1)
serversocket.setblocking(0)

epoll = select.epoll()
epoll.register(serversocket.fileno(), select.EPOLLIN)

try:
   connections = ; requests = ; responses = 
   while True:
      events = epoll.poll(1)
      for fileno, event in events:
         if fileno == serversocket.fileno():
            connection, address = serversocket.accept()
            connection.setblocking(0)
            epoll.register(connection.fileno(), select.EPOLLIN)
            connections[connection.fileno()] = connection
            requests[connection.fileno()] = b‘‘
            responses[connection.fileno()] = response
         elif event & select.EPOLLIN:
            requests[fileno] += connections[fileno].recv(1024)
            if EOL1 in requests[fileno] or EOL2 in requests[fileno]:
               epoll.modify(fileno, select.EPOLLOUT)
               print(-*40 + \\n + requests[fileno].decode()[:-2])
         elif event & select.EPOLLOUT:
            byteswritten = connections[fileno].send(responses[fileno])
            responses[fileno] = responses[fileno][byteswritten:]
            if len(responses[fileno]) == 0:
               epoll.modify(fileno, 0)
               connections[fileno].shutdown(socket.SHUT_RDWR)
         elif event & select.EPOLLHUP:
            epoll.unregister(fileno)
            connections[fileno].close()
            del connections[fileno]
finally:
   epoll.unregister(serversocket.fileno())
   epoll.close()
   serversocket.close()

详情见:https://www.cnblogs.com/9527chu/p/5661932.html

 

以上是关于IO多路复用的主要内容,如果未能解决你的问题,请参考以下文章

你管这破玩意叫 IO 多路复用?

多路转接(IO复用)接口介绍

多路复用io接口-epoll

IO多路复用 -- selectpollepoll实现TCP反射程序

IO多路复用

经典5种IO模型 | IO多路复用