python epoll實現非同步socket

來源:互聯網
上載者:User

標籤:timeout   available   rom   disable   pen   bsp   支援   ipaddress   linu   

一、同步和非同步:

在程式執行中,同步運行意味著等待調用的函數、線程、子進程等的返回結果後繼續處理;非同步指不等待當下的返回結果,直接運行主進程下面的程式,等到有返回結果時,通知主進程處理。有點高效。

二、epoll實現非同步網路通訊:

首先epoll只支援linux下的python。

服務端實現epoll非同步主要流程就是如下代碼,講解將在代碼裡面書寫:

 1 # -*- coding:utf -*- 2  3 import socket 4 import select 5 ‘‘‘ 6 需要用到的lib檔案: 7 socket、select 8 ‘‘‘ 9 if __name__ == "__main__":10     server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)11     server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)#IP地址連接埠複用12     ipaddress = "127.0.0.1"13     port = 3344514     address = (ipaddress,port)15     serverfd = server.fileno()16     server.bind(address)17     serverdict = {}18     serverdict[serverfd] = server19     epoll = select.epoll()#建立epoll對象20     epoll.register(serverfd,select.EPOLLIN)#註冊訊息類型(輸入)21     server.listen(5)22     while True:23         events = epoll.poll(1)#建立事件隊列24         for fileno,event in events:25             if fileno == serverfd:26                 (client,address) = socket.accept()27                 print "<-----",client,"----",address,"----->"28                 client.setblocking(0)29                 epoll.register(client.fileno(),select.EPOLLIN)#註冊事件隊列30                 serverdict[client.fileno()] = client31             elif event & select.EPOLLIN:#當有事件時候處理32                 print "client:",fileno33                 data = serverdict[fileno].recv(4096)34                 print data35                 

核心步驟如下:

 1 #建立epoll 2 epoll = select.epoll() 3 #註冊事件隊列 4 epoll.register(socketname.filefd,select.EPOLLIN)#EPOLLIN是事件類型 5 #建立事件隊列: 6 events = epoll.poll(1) 7 #事件隊列的資料結構: 8 #(檔案對象描述符,事件訊息) 9 #檢測事件 進行處理:10 for (fd,event) in events:11     if event & select.EPOLLIN:12         do_somethings()13 #常用事件類型:14 ‘‘‘15 EPOLLIN    Available for read16 EPOLLOUT    Available for write17 EPOLLPRI    Urgent data for read18 EPOLLERR    Error condition happened on the assoc. fd19 EPOLLHUP    Hang up happened on the assoc. fd20 EPOLLET    Set Edge Trigger behavior, the default is Level Trigger behavior21 EPOLLONESHOT    Set one-shot behavior. After one event is pulled out, the fd is internally disabled22 EPOLLRDNORM    Equivalent to EPOLLIN23 EPOLLRDBAND    Priority data band can be read.24 EPOLLWRNORM    Equivalent to EPOLLOUT25 EPOLLWRBAND    Priority data may be written.26 EPOLLMSG    Ignored.27 ‘‘‘

其他常用的函數:

1 epoll.close()2 epoll.fileno()#返回epoll對象的檔案描述符3 epoll.fromfd(fd)#從給定對象建立一個epoll對象4 epoll.modify(fd,eventmask)#修改檔案檔案描述對象的epoll事件類型5 epoll.unregister(fd)取消註冊6 epoll.poll(timeout=xxx,maxevents=xxx)#參數均為非必填項

 

python epoll實現非同步socket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.