We can understand how Apache works in this way.
1 single-Process TCP service (plug-in)
This is the most primitive service, that is, can only handle a client connection, and so on when the current client shuts down before processing the next client, is a blocking wait
fromSocket Import *Sersocket=sockets (Af_inet, Sock_stream) #重复使用绑定的信息serSocket. setsockopt (Sol_socket, SO_REUSEADDR,1) Localaddr= ("',7788) Sersocket.bind (localaddr) Sersocket.listen (5) whileTrue:print ('-----The main process, waiting for the client to connect------') newsocket,destaddr=sersocket.accept () print ('-----. The main process, and then the data processing [%s]-----'%str (DESTADDR))Try: whileTrue:recvdata= Newsocket.recv (1024x768) ifLen (RecvData) >0: Print ('recv[%s]:%s'%(str (DESTADDR), recvdata))Else: Print ('[%s] client has closed ...'%str (DESTADDR)) Break finally: Newsocket.close ()
This type of blocking nature is not suitable for processing multi-client requests, so there is a revision
2 Multi-process services
Multi-Client connection requests are processed by multiple processes, and the single process is optimized.
fromSocket Import * fromMultiprocessing Import * fromTime Import sleep# processes the client's request and serves it with Def dealwithclient (newsocket,destaddr): whileTrue:recvdata= Newsocket.recv (1024x768) ifLen (RecvData) >0: Print ('recv[%s]:%s'%(str (DESTADDR), recvdata))Else: Print ('[%s] client is closed'%str (DESTADDR)) BreakNewsocket.close () def main (): Sersocket=socket (af_inet, Sock_stream) sersocket.setsockopt (Sol_socket, SO_REUSEADDR,1) Localaddr= ("',7788) Sersocket.bind (localaddr) Sersocket.listen (5) Try: whileTrue:print ('-----The main process, wait for the new client to arrive------') newsocket,destaddr=sersocket.accept () print ('-----The main process, next create. A new process is responsible for data processing [%s]-----'Client= Process (Target=dealwithclient, args=(NEWSOCKET,DESTADDR)) Client.start () #因为已经向. The process was copied. Part (citation), and. This socket is useless in the process #所以关闭 Newsock Et.close ()finally: #当为所有的客户端服务完之后再进. Close to the link that no longer receives the new client Sersocket.close ()if__name__ = ='__main__': Main ()
By creating a process for each client, the ability to service multiple clients at the same time, when the client is not particularly large, this way is OK, if there are hundreds of thousands, it is not available, because each creation process consumes more resources, so there is an improved version
3 Multi-Threading service
Multi-Client connection requests with multithreading, because threads share resources, do not replicate as many resources as a process, so processing is faster.
#coding =utf-8 fromSocket Import * fromThreading Import Thread fromTime Import sleep# processes the client's request and executes the Def dealwithclient (newsocket,destaddr): whileTrue:recvdata= Newsocket.recv (1024x768) ifLen (RecvData) >0: Print ('recv[%s]:%s'%(str (DESTADDR), recvdata))Else: Print ('[%s] client is closed'%str (DESTADDR)) BreakNewsocket.close () def main (): Sersocket=socket (af_inet, Sock_stream) sersocket.setsockopt (Sol_socket, SO_REUSEADDR,1) Localaddr= ("',7788) Sersocket.bind (localaddr) Sersocket.listen (5) Try: whileTrue:print ('-----The main process, wait for the new client to arrive------') newsocket,destaddr=sersocket.accept () print ('-----The main process, next create. A new process is responsible for data processing [%s]-----'Client= Thread (Target=dealwithclient, args=(NEWSOCKET,DESTADDR)) Client.start () #这里不要关闭, thread sharing resources, shutting down causes all threads to shut down #newSocket. Close () finally: Sersocket.close ()if__name__ = ='__main__': Main ()
Python simple build blocking single-process, multi-process, multi-threaded service