1. 程式人生 > >IO多路復用select/epoll詳解以及在Python中的應用

IO多路復用select/epoll詳解以及在Python中的應用

end 內核 exc 阻塞 明顯 __name__ 請求 urlparse select()

IO multiplexing(IO多路復用)

IO多路復用,有些地方稱之為event driven IO(事件驅動IO)。
技術分享圖片

它的好處在於單個進程可以處理多個網絡IO請求。select/epoll這兩個是函數,它會不斷輪詢所有的socket,直到某個socket就緒有數據可達,就會通知用戶進程,當用戶進程調用了select函數,select是一個阻塞方法,會把進程阻塞住,同時會監聽所有select負責的socket,當任何一個socket中的數據準備好了,select就會返回。這個時候用戶進程再調用readRecv操作,將數據從內核拷貝到用戶進程。

select雖然是阻塞的,但是它的優勢在於它可以用一個進程處理多個連接,這個利用非阻塞的輪詢方式是無法實現的,當連接數增多時優勢就明顯,而對於單個連接則跟同步IO區別不大甚至性能還要更低。

下面用Python的socket編程模擬IO多路復用(IO多路復用+回調+事件循環)

class Fetcher:
    def connected(self, key):
        selector.unregister(key.fd)
        self.con.send('GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n'.format(self.path,self.host).encode('utf-8'))
        selector.register(self.con.fileno(), EVENT_READ, self.read)

    def read(self, key):
        d = self.con.recv(1024)
        if d:
            print(d)
            self.data += d
        else:
            selector.unregister(key.fd)
            self.data = self.data.decode('utf-8')
            html_data = self.data.split('\r\n\r\n')[1]
            print(html_data)
            self.con.close()

    def get_url(self, url):
        ...
        self.con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.con.setblocking(False)
        #設置非阻塞
        try:
            self.con.connect((self.host, 80))
        except BlockingIOError as e:
            pass
        selector.register(self.con.fileno(), EVENT_WRITE, self.connected)

過程:發送一個socket請求設置為非阻塞,在select函數中註冊事件,self.con.fileno表示當前連接在進程中的描述符,EVENT_WRITE表示socket準備是否就緒,self.connected為回調函數,準備完成後就調用。selector.unregister(key.fd)取消註冊,發送HTTP請求,再調用selector.register(self.con.fileno(), EVENT_READ, self.read)註冊,若當前請求內容可讀,則調用read回調函數讀取出響應內容。

註明:在windows下會調用select函數,而在linux/unix下則會調用epoll函數

完整代碼如下:

import socket
from urllib.parse import urlparse
from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
selector = DefaultSelector()

class Fetcher:
    def connected(self, key):
        selector.unregister(key.fd)
        self.con.send('GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n'.format(self.path,self.host).encode('utf-8'))
        selector.register(self.con.fileno(), EVENT_READ, self.read)

    def read(self, key):
        d = self.con.recv(1024)
        if d:
            print(d)
            self.data += d
        else:
            selector.unregister(key.fd)
            self.data = self.data.decode('utf-8')
            html_data = self.data.split('\r\n\r\n')[1]
            print(html_data)
            self.con.close()

    def get_url(self, url):
        url = urlparse(url)
        self.host = url.netloc
        self.path = url.path
        self.data = b''
        if self.path == "":
            self.path = '/'

        self.con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.con.setblocking(False)

        try:
            self.con.connect((self.host, 80))
        except BlockingIOError as e:
            pass
        #註冊
        selector.register(self.con.fileno(), EVENT_WRITE, self.connected)

def loop():
    while True:
        ready = selector.select()
        for key, mask in ready:
            callback = key.data
            callback(key)

if __name__ == '__main__':
    fetcher = Fetcher()
    fetcher.get_url('http://www.baidu.com')
    loop()

IO多路復用select/epoll詳解以及在Python中的應用