select programming [+ callback event loop]

Also I feel sad to write than java .....

 

"" "
 Select, poll, epoll 
Note: epoll is not necessarily better than the performance of select, which requires a look at the scene
 1 . In high concurrency scenarios, and connection activity is not very high, epoll better than select, such as web applications
 2 in the concurrent is not high, but under very active connection scene, select better than epoll, such as games 

programming mode: the SELECT + + cycle event callback 

"" "
 Import socket 
from urllib.parse Import urlparse 
when # using DefaultSelector, python based application internet automatic selection mode or select mode epoll 
# DefaultSelector the select and epoll is encapsulated 
from selectors Import DefaultSelector, EVENT_READ, EVENT_WRITE 

Selector = DefaultSelector () 

# http request is completed using the select (window does not support epoll) 
URLs = [ "http: // www.baidu.com "]
stop = False # set a global variable, the control loop ends, or being given window environment 


class Fetcher for: 
    DEF GET_URL (Self, URL):
         '' 'http request is completed by the socket' '' 
        self.url = URL 
        URL = the urlparse (url) 
        self.host = url.netloc 
        self.path = url.path
         IF self.path == "" : 
            self.path = "/" 
        # establish a socket connection 
        self.client = socket.socket (socket.AF_INET, socket .SOCK_STREAM) 
        self.client.setblocking (False) arranged non-blocking mode # 
        self.data = B ""  # Bytes representing
         the try : 
            self.client.connect ((self.host, 80 )) 
        the except BlockingIOError AS E: 
            Pass 
        # register, monitor write event callbacks 
        selector.register (self.client.fileno (), EVENT_WRITE, self.writable ) 

    DEF Writable (Self, Key):
         '' 'write callback' '' 
        selector.unregister (key.fd) 
        self.client.send (
             "{} the GET the HTTP / 1.1 \ R & lt \ NHOst: {} \ R & lt \ nConnection : use Close \ r \ the n-\ r \ the n-".format (self.path, self.host) .encode (" UTF-8 " )) 
        # registration, read listener event callback 
        selector.register (self.client.fileno () , EVENT_READ, self.readable) 

    DEF readable's (Self, Key): 

        DSelf.client.recv = (1024 )
         IF D: 
            self.data + = D
         the else : # data has been read 
            selector.unregister (key.fd) 
            Data = self.data.decode ( "UTF-. 8" ) 
            html_data = Data .split ( '\ R & lt \ n-\ R & lt \ n-') [. 1 ] 
            Print (html_data) 
            self.client.close () 
            urls.remove (self.url) # processed after a url, url will be removed from the urls in addition to 
            IF not urls: # urls if there is no data, and you can stop the event loop cycle 
                , Ltd. Free Join sTOP 
                sTOP = True 


DEF loop (): 
    '' '
    Event loop, the socket stop request status, and calls the corresponding callback function 

    : return :
     '' '
     #. 1 select register mode does not support itself, but to select DefaultSelector is encapsulated. 
    # 2 subsequent state change callbacks socket. is done by the program, not by the operating system to complete
     the while not STOP: 
        READY = selector.select ()
         for Key, mask in READY: 
            call_back = key.data 
            call_back (Key) 


IF __name__ == '__main__' : 
    Fetcher = Fetcher for () 
    fetcher.get_url ( 'http://www.baidu.com' ) 
    Loop ()

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12078683.html