We can understand how apache works in this way
1 Single-process TCP service (blocking)
This is the most primitive service, that is to say, it can only handle the connection of one client, and after the current client closes, it can only handle the next client, which is a blocking wait.
from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # Reuse bound information (SOL_SOCKET, SO_REUSEADDR , 1) localAddr = ('', 7788) (localAddr) (5) while True: print('----- master process, waiting for client connection ------') newSocket,destAddr = () print('-----. Main process, next responsible for data processing [%s]-----'%str(destAddr)) try: while True: recvData = (1024) if len(recvData)>0: print('recv[%s]:%s'%(str(destAddr), recvData)) else: print('[%s] Client has closed ...'%str(destAddr)) break finally: ()
This blocking type is naturally not suitable for handling multi-client requests, so there is a revamped
2 Multi-process services
Taking multiple processes to handle multi-client connection requests is optimized for single processes.
from socket import * from multiprocessing import * from time import sleep # Process and service client requests def dealWithClient(newSocket,destAddr): while True: recvData = (1024) if len(recvData)>0: print('recv[%s]:%s'%(str(destAddr), recvData)) else: print('[%s] Client has closed'%str(destAddr)) break () def main(): serSocket = socket(AF_INET, SOCK_STREAM) (SOL_SOCKET, SO_REUSEADDR , 1) localAddr = ('', 7788) (localAddr) (5) try: while True: print('----- master process,, waiting for new clients to arrive ------') newSocket,destAddr = () print('----- master process,, next create . . a new process for data processing [%s]-----' client = Process(target=dealWithClient, args=(newSocket,destAddr)) () #Because it has been copied to the . . process has been copied to . (cite .) and the socket is no longer useful in the . process, the socket is no longer useful. # So close () finally: # Enter when done serving all clients. Close to indicate that no more links will be received from new clients () if __name__ == '__main__': main()
By creating a process for each client, it is possible to serve multiple clients at the same time; when the number of clients is not particularly large, this approach is fine, but if there are hundreds of thousands, it is not desirable because each time a process is created, it consumes more resources, so there is an improved version of the
3 Multi-threaded services
Multi-threading is used to handle multi-client connection requests, which are processed faster because the threads share resources and don't have to replicate out multiple resources like processes do.
#coding=utf-8 from socket import * from threading import Thread from time import sleep # Process client requests and execute def dealWithClient(newSocket,destAddr): while True: recvData = (1024) if len(recvData)>0: print('recv[%s]:%s'%(str(destAddr), recvData)) else: print('[%s] Client has closed'%str(destAddr)) break () def main(): serSocket = socket(AF_INET, SOCK_STREAM) (SOL_SOCKET, SO_REUSEADDR , 1) localAddr = ('', 7788) (localAddr) (5) try: while True: print('----- master process,, waiting for new clients to arrive ------') newSocket,destAddr = () print('----- master process,, next create . . a new process for data processing [%s]-----' client = Thread(target=dealWithClient, args=(newSocket,destAddr)) () #Don't close it here, threads share resources, closing it will cause all threads to close. #() finally: () if __name__ == '__main__': main()
Above this python simple to build a blocking single process, multi-process, multi-threaded service example is all I have to share with you, I hope to be able to give you a reference, and I hope that you support me more.