Day15-python Basic 15 Module Learning-selectors

Source: Internet
Author: User
Tags epoll

The content of this section

1:python/selectors Module

2:selsect instances

1:python/selectors Modules and queues

The 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.

Common IO multiplexing is divided into three types: Select, poll, Epoll, and only select is supported on Windows, while Linux supports

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

2:selsect instances:
1 ## #服务端 ################2 3 ImportSelectors#IO multiplexing based on the Select module, it is recommended that you use4 ImportSocket5sock=Socket.socket ()6Sock.bind (('127.0.0.1', 8800))7Sock.listen (5)8 sock.setblocking (False)9Sel=selectors. Defaultselector ()#Choose the best IO multiplexing mechanism based on the platform, e.g. Linux will choose EpollTen  One defRead (conn,mask): A     Try: -DATA=CONN.RECV (1024) -         Print(Data.decode ('UTF8')) theData2=input ('>>>>') -Conn.send (Data2.encode ('UTF8')) -     exceptException: - Sel.unregister (conn) +  - defAccept (sock,mask): +Conn,addr=sock.accept () A     Print('-------', conn) at Sel.register (conn,selectors. Event_read,read) -  -Sel.register (sock, selectors. Event_read, accept)#registration function, once the reaction continues to execute down -  -  whileTrue: -     Print('wating ....') inEvents=sel.select ()#The Listener Object [(sock), (), ()] of the event and select is monitored -  to      forKey,maskinchEvents: +         #print (key.data) #accept Find the active binding function -         #print (key.fileobj) #sock Find the active file descriptor the  *Func=Key.data $obj=Key.fileobjPanax Notoginseng  -Func (Obj,mask)#1 Accept (Sock,mask) 2read (conn,mask) the  + ##客户端 ################# A ImportSocket thetin=Socket.socket () +Tin.connect (('127.0.0.1', 8800)) -  whileTrue: $Inp=input ('>>>>') $Tin.send (Inp.encode ('UTF8')) -DATA=TIN.RECV (1024) -     Print(Data.decode ('UTF8'))

Day15-python Basic 15 Module Learning-selectors

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.