This paper analyzes the principle and usage of Asyncore module in Python by example, and shares it for everyone's reference. The specific analysis is as follows:
The Asyncore library is a standard library of Python, which is the wrapper for an asynchronous socket. While we operate the network, we can directly use the underlying libraries such as sockets, but Asyncore makes it easier to operate the network and avoid the complexities of using tools such as Socket,select,poll directly.
This library is simple and contains a function and a class
* Loop () function
* Dispatcher base class
It is important to note that the loop function is global, not a dispatcher method
Every object of a class inherited from dispatcher can be seen as a socket we need to handle, either TCP or UDP, or even other infrequently used. Easy to use, we need to define a class that inherits dispatcher, and then we rewrite (overwrite) some of the methods.
The method we need to rewrite is usually preceded by Handle_.
Class Refuse (Dispatcher): def handle_accept (): #do nothing ... Pass
The loop () function is responsible for detecting an instance of a dict,dict saved dispatcher, which is called the channel. Each time you create a Dispatcher object, you will add yourself to a default Dict (you can also specify the channel yourself). When the object is added to the channel, the socket behavior is defined, and the program only needs to call loop (), and all functions are implemented.
Asyncore is a good design in the Python standard library
In Python's standard document, there is an example of Asyncore
Import Asyncore, Socketclass http_client (asyncore.dispatcher): def __init__ (self, Host, path): Asyncore.dispatcher.__init__ (self) self.create_socket (socket.af_inet, socket. SOCK_STREAM) Self.connect ((host)) Self.buffer = ' GET%s http/1.0\r\n\r\n '% path def handle_connect ( Self): pass def handle_close (self): self.close () def handle_read (self): print Self.recv (8192 ) def writable (self): return (len (Self.buffer) > 0) def handle_write (self): sent = Self.send ( Self.buffer) Self.buffer = Self.buffer[sent:]c = Http_client (' www.python.org ', '/') Asyncore.loop ()
Run this function, found that python.org's homepage was downloaded, that is to say we implemented an HTTP layer protocol? But we're just using the socket-level API ... So take a look at the mystery of these lines of code!
Writable and readable are called when a socket can be written or detected when the data arrives, and a bool is returned to determine whether to handle_read or handle_write
Opening asyncore.py You can see that the methods defined in the dispatcher class writable and readable are fairly simple to define:
def readable (self): return truedef writable (self): return True
That is, once readable or writable is detected, Handle_read/handle_write is called directly, but in the example above, we see an overload that looks like a C + + virtual function, doesn't it? )
def writable (self): return (len (Self.buffer) > 0)
Obviously, when we have the data need to send, we only give writable caller return a true, so do not need to judge in Handle_write, logic is clear, the code is clear, the drawback is that understanding takes a little time, but not difficult!
The rest of the code looks very clear and there is a sense that a soldier will block. When an HTTP server sends processing to complete your request, close the socket, our Handle_close () also completes its mission accordingly. Close () Removes the object itself from the channel and is responsible for destroying the socket object.
def close (self): self.del_channel () self.socket.close ()
The loop () function detects an empty channel, exits the loop, and the program completes the task, exit.
Hopefully this article will help you with Python programming.