Python Epoll implementing asynchronous sockets

Source: Internet
Author: User

One, synchronous and asynchronous:

In program execution, running synchronously means waiting for the returned result of the called function, thread, subprocess, etc. to continue processing, asynchronously means to run the program under the main process without waiting for the immediate return result, and then notifies the main process when there is a return result. A little more efficient.

Second, epoll implementation of asynchronous network communication:

First, Epoll only supports Python under Linux.

The main process of implementing Epoll Async on the server is the following code, which will be written in the code:

1 #-*-Coding:utf-*-2 3 ImportSocket4 ImportSelect5 " "6 The lib file you need to use:7 socket, select8 " "9 if __name__=="__main__":TenServer =Socket.socket (Socket.af_inet,socket. SOCK_STREAM) OneServer.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)#IP Address Port multiplexing AIPAddress ="127.0.0.1" -Port = 33445 -Address =(Ipaddress,port) theSERVERFD =Server.fileno () - Server.bind (address) -Serverdict = {} -SERVERDICT[SERVERFD] =Server +Epoll = Select.epoll ()#Create a Epoll object -Epoll.register (Serverfd,select. Epollin)#Register message type (input) +Server.listen (5) A      whileTrue: atEvents = Epoll.poll (1)#Create event Queue -          forFileno,eventinchEvents: -             ifFileno = =SERVERFD: -(client,address) =socket.accept () -                 Print "<-----", Client,"----", Address,"----->" - client.setblocking (0) inEpoll.register (Client.fileno (), select. Epollin)#Registering event Queues -Serverdict[client.fileno ()] =Client to             elifEvent & Select. Epollin:#when there are events to deal with +                 Print "Client:", Fileno -data = SERVERDICT[FILENO].RECV (4096) the                 PrintData *                 

The core steps are as follows:

1 #Create Epoll2Epoll =Select.epoll ()3 #Registering event Queues4Epoll.register (Socketname.filefd,select. Epollin)#Epollin is an event type5 #To create an event queue:6Events = Epoll.poll (1)7 #data structure of the event queue:8 #(File object descriptor, event message)9 #Detect events for processing:Ten  for(fd,event)inchEvents: One     ifEvent &Select. Epollin: A do_somethings () - #Common event Types: - " " the Epollin Available for Read - epollout Available for write - EPOLLPRI Urgent data for read - Epollerr Error condition happened on the Assoc. FD + Epollhup hang to happened on the Assoc. FD - epollet Set Edge Trigger behavior, the default is level Trigger behavior + epolloneshot Set one-shot behavior. After one event was pulled out, the FD is internally disabled A epollrdnorm equivalent to Epollin at Epollrdband Priority Data Band can be read. - epollwrnorm equivalent to Epollout - Epollwrband priority data is written. - Epollmsg ignored. - " "

Other commonly used functions are:

1 epoll.close () 2 Epoll.fileno ()# Returns the Epoll object's file descriptor 3 epoll.fromfd (FD)#  Creates a Epoll object from a given object 4 epoll.modify (fd,eventmask)# Modify the Epoll event type of the file description object 5 Epoll.unregister (FD) de-registration 6 epoll.poll (timeout=xxx,maxevents=xxx)# parameters are not required

Python Epoll implementing asynchronous sockets

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.