Python SocketServer.py 源码分析(三)

前面我们见识了实现TCPServer的方法。最早关于介绍BaseServer的时候,我们知道python对BaseServer设计的时候,预留了可用于Mixin扩展多线程或多进程的接口。mixin通过复写父类的parse_request方法实现。

ThreadingMixIn

ThreadingMixIn 类实现了多线程的方式,它只有两个方法,分别是process_request和 process_request_thread方法。多进程的方式是ForkingMixIn,暂且略过。

process_request

def process_request(self, request, client_address):
    t = threading.Thread(target = self.process_request_thread,
                         args = (request, client_address))
    t.daemon = self.daemon_threads
    t.start()

process_request方法复写了父类的此方法。以此为接口入口,对每一个请求,调用Thread开启一个新的线程。每一个线程都绑定process_request_thread方法。

process_request_thread

    def process_request_thread(self, request, client_address):
        try:
            self.finish_request(request, client_address)
            self.shutdown_request(request)
        except:
            self.handle_error(request, client_address)
            self.shutdown_request(request)

process_request_thread方法和BaseServer里的parse_request几乎一样。只不过是多线程的方式调用。

使用的时候,通过多继承调用接口,例如:

class ThreadingTCPServer(ThreadingMixIn, TCPServer): 
    pass

具体的调用过程如下:


ThreadingMixIn -- TCPServer - StreamRequestHandler

__init__(server_address, RequestHandlerClass): 
    BaseServer.server_address
    BaseServer.RequestHandlerClass

    TCPServer.socket = socket.socket(self.address_family, self.socket_type)
    TCPServer.server_bind()
    TCPServer.server_activate()

serve_forever(): 

    select() 

    BaseServer._handle_request_noblock()

        TCPServer.get_request() -> request, client_addres
            socket.accept()

        BaseServer.verify_request()

            BaseServer.process_request()

                ThreadingMixIn.process_request()
                    t = threading.Thread(target = ThreadingMixIn.process_request_thread)

                    ThreadingMixIn.process_request_thread

                        BaseServer.finish_request(request, client_address)

                            BaseServer.RequestHandlerClass()

                                BaseRequestHandler.__init__(request)

                                    BaseRequestHandler.request
                                    BaseRequestHandler.client_address = client_address

                                    StreamRequestHandler.setup()

                                        StreamRequestHandler.connection = StreamRequestHandler.request
                                        StreamRequestHandler.rfile
                                        StreamRequestHandler.wfile

                                    BaseRequestHandler.handle()

                                    StreamRequestHandler.finsih()
                                        StreamRequestHandler.wfile.close()
                                        StreamRequestHandler.rfile.close()

                        BaseServer.shutdown_request(request)
                            TCPServer.shutdown()
                                request.shutdown()
                            TCPServer.close_request(request)
                                request.close()

            TCPServer.shutdown_request(request)
                TCPServer.shutdown(request)
                    request.shutdown()
                TCPServer.close_request(request)
                    request.close()

你可能感兴趣的:(Python SocketServer.py 源码分析(三))