Python/selectors Module

Source: Internet
Author: User
Tags epoll

The Python/selectors module selectors module is an IO multiplexing mechanism that can be implemented:

It has a platform to choose the best IO multipath mechanism, such as on the win system he default is the Select mode and on Linux it default epoll.

Commonly divided into three kinds:

Select, poll, Epoll

Disadvantages of Select:

1, each call will be all the file descriptor (FD) copy of the kernel space, resulting in efficiency degradation

2. Traverse all file descriptors (FD) to see if there is any data access

3. Maximum number of links (1024)

Poll

It is the transition phase of select and Epoll, which has no limit on the maximum number of links

Epoll:

1. The first function is to create a epoll handle that copies all descriptors (FD) to the kernel space, but copies it only once.

2, the callback function, a function or an action after the successful completion of the function will be triggered for all descriptors (FD) binding a callback function, once the data access is triggered by the callback function, the callback function will (FD) placed in the list

3. function to determine whether the linked list is empty

4. No limit on Maximum startup item

Selsect instances:

1 Service Side2 ImportSelectors#IO multiplexing based on the Select module, it is recommended that you use3 ImportSocket4sock=Socket.socket ()5Sock.bind (('127.0.0.1', 8800))6Sock.listen (5)7 sock.setblocking (False)8Sel=selectors. Defaultselector ()#Choose the best IO multiplexing mechanism based on the platform, e.g. Linux will choose Epoll9 Ten defRead (conn,mask): One     Try: ADATA=CONN.RECV (1024) -         Print(Data.decode ('UTF8')) -Data2=input ('>>>>') theConn.send (Data2.encode ('UTF8')) -     exceptException: - Sel.unregister (conn) -  + defAccept (sock,mask): -Conn,addr=sock.accept () +     Print('-------', conn) A Sel.register (conn,selectors. Event_read,read) atSel.register (sock, selectors. Event_read, accept)#Registration Function -  whileTrue: -     Print('wating ....') -Events=sel.select ()#[(sock), (), ()] monitoring -  -      forKey,maskinchEvents: in         #print (key.data) #accept Find the active binding function -         #print (key.fileobj) #sock Find the active file descriptor to  +Func=Key.data -obj=Key.fileobj the  *Func (Obj,mask)#1 Accept (Sock,mask) 2read (conn,mask) $------------------------------------------------------------------------------Panax Notoginseng Client - ImportSocket thetin=Socket.socket () +Tin.connect (('127.0.0.1', 8800)) A  whileTrue: theInp=input ('>>>>') +Tin.send (Inp.encode ('UTF8')) -DATA=TIN.RECV (1024) $     Print(Data.decode ('UTF8'))

Python/selectors Module

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.