Python full stack development Day36-io multiplexing

Source: Internet
Author: User
Tags epoll file handling

First, review 1, process, thread, the process: the smallest resource allocation unit in the computer, data isolation, you can use multi-core, data is not a security thread: is the smallest CPU scheduling unit in the computer, data sharing, GIL, data Insecure association: is a part of the thread, is by the user To dispatch, data sharing, data security 2, synchronous, asynchronous, blocking, non-blocking

Async: Doing more than one thing at the same time

Synchronization: One thing is done and then the next.

Blocking: Recv\recvfrom\accept\sleep\input

Non-blocking:

Second, IO multiplexing

IO operation:

File handling: File processing, json.dump/load,input,print,logging

Network operation: Recv/send,resvfrom/sendto,accept/connect

# Why RECV to block
# Waiting for data to come into my Python program's memory

1. Blocking IO:

      

      

2. Non-blocking IO:

      

      

code example:

      

#Import TimeImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 9000) ) sk.setblocking (False) sk.listen () Conn_lst=[]del_lst= [] whileTrue:Try: Conn,addr= Sk.accept ()#--Non-blocking, no connection to errorconn_lst.append (conn)Print(conn)exceptBlockingioerror: forConinchConn_lst:#conn1,conn2,conn3            Try: con.send (b'Hello')                Try:                    Print(CON.RECV (1024))#non-blocking no message to error                exceptBlockingioerror:Pass   #recv No news of the error            exceptConnectionreseterror:#send error with no connectioncon.close () del_lst.append (Con) forConinchDel_lst:conn_lst.remove (Con) del_lst.clear ()#The non-blocking form implements the concurrency of the socket server#The non-blocking form implements the concurrent socket server, which consumes too much CPU#The high-speed processing of the program takes up CPU resources when there is no data .
non-blocking io-server
Import= socket.socket () sk.connect('127.0.0.1', 9000) while   True:    Print(SK.RECV (1024x768))    Sk.send (b' Bye ' ) Sk.close ()
non-blocking ioclient

3.IO Multiplexing:

      

      

ImportSelectImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 9000) ) sk.setblocking (False) sk.listen () Rlst= [SK]#Listening is the read operation of the objectWlst = []#Listening is the write operation of the objectXlst = []#The listener is an exception operation for the object whileTRUE:RL,WL,XL= Select.select (Rlst,wlst,xlst)#[Sk,conn]     forObjinchRl:#[CONN1,CONN2]        ifobj = =sk:conn,addr= Sk.accept ()#each time the connection is established Connrlst.append (conn)Else: Msg= OBJ.RECV (1024)            ifmsg = = B"': Obj.close () rlst.remove (obj)Continue            Print(msg) obj.send (b'Hello')#Socketserver#concurrent operation of TCP protocol selectors + multithreading
Multiplexing Io-select-sever
ImportSocketsk=socket.socket () Sk.connect ('127.0.0.1', 9000)) whileTrue:sk.send (b'Wahaha')    Print(SK.RECV (1024) ) Sk.close ()#the working mechanism of IO multiplexing Select#Select Windows Polling#Poll Linux Polling, poll can listen more objects than select#Epoll Linux is not a polling method, but instead uses the form of a callback function
Multiplexing io-select-client


# The working mechanism of IO multiplexing Select
# Select Windows Polling
# Poll Linux Polling, poll can listen more objects than select
# Epoll Linux is not polled, but instead takes the form of a callback function

Cross-platform or platform-adaptive IO multiplexing:

ImportSelectorsImportSocketsel=selectors. Defaultselector ()defAccept (obj,mask):"""callback function that is invoked when the selectors instance senses that a user is connected to the server. :p Aram obj::p Aram Mask:: return:"""conn,addr=obj.accept () sel.register (conn, selectors. Event_read, READ)#registering the user connection conn to the selector listener listdefRead (conn,mask):"""callback function that is invoked when the selectors instance senses that a user is sending data. :P Aram Conn::p Aram Mask:: return:"""    Try: Data= CONN.RECV (1024)        if  notData#Null throws an exception handled by the exception handling statement below            RaiseException conn.send (Data+'_SB'. Encode ('Utf-8'))    exceptException as E:Print('closing', Conn) sel.unregister (conn) conn.close () SK=Socket.socket () sk.setsockopt (socket). Sol_socket, SOCKET. SO_REUSEADDR,1) Sk.bind ('127.0.0.1', 9000) ) Sk.listen () sk.setblocking (False) sel.register (SK, selectors. Event_read, accept) while1: Events= Sel.select ()#[Sk,conn1,conn2 ...] who has the new data will return who     forKey, MaskinchEvents:callback= Key.data#Back to functionCallback (Key.fileobj, mask)#Execute callback function
Selectors-server
 import   Socketsk  = Socket.socket () Sk.connect (( "   127.0.0.1   ", 9000))  while  1 = input ("  >>>   " ) sk.send (Inp.encode (  " utf-8  "   print  (Sk.recv (1024x768). Decode ("  utf-8   )) 
selectors-client

4. Asynchronous IO:

      

5. Various IO comparisons:

      

Python full stack development Day36-io multiplexing

Related Article

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.