Python simple construction of blocking single process, multi-process, multi-thread service, python multi-thread
We can understand the working principle of apache in this way.
1 single-process TCP Service (congested)
This is the most primitive service, that is, it can only process connections from clients. After the current client is closed, it can process the next client. It is a blocking wait.
From socket import * serSocket = socket (AF_INET, SOCK_STREAM) # repeat the bound information serSocket. setsockopt (SOL_SOCKET, SO_REUSEADDR, 1) localAddr = ('', 7788) serSocket. bind (localAddr) serSocket. listen (5) while True: print ('----- main process, waiting for client connection ------') newSocket, destAddr = serSocket. accept () print ('-----. the main process is responsible for data processing [% s] ----- '% str (destAddr) try: while True: recvData = newSocket. recv (1024) if len (recvData)> 0: print ('recv [% s]: % s' % (str (destAddr), recvData) else: print ('[% s] The client has been closed... '% str (destAddr) break finally: newSocket. close ()
This blocking type is not suitable for processing requests from multiple clients, so it has been revised.
2 multi-process service
Multi-process processing for multi-client connection requests is adopted to optimize a single process.
From socket import * from multiprocessing import * from time import sleep # process client requests and serve them def dealWithClient (newSocket, destAddr): while True: recvData = newSocket. recv (1024) if len (recvData)> 0: print ('recv [% s]: % s' % (str (destAddr), recvData) else: print ('[% s] The client has closed' % str (destAddr) break newSocket. 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: while True: print ('----- main process, wait for the arrival of the new client ------') newSocket, destAddr = serSocket. accept () print ('----- main process, and then create. new processes are responsible for processing data [% s] ----- 'client = Process (target = dealWithClient, args = (newSocket, destAddr) client. start () # because it has been directed. copied in the process. parts (cited .), and. this socket is useless in the process. # Close newSocket. close () finally: # after serving all clients, enter. closed, indicating that the new client's link serSocket is no longer received. close () if _ name _ = '_ main _': main ()
By creating a process for each client, you can serve multiple clients at the same time. When there are not many clients, this method is okay. If there are hundreds of clients, it is not desirable, because every process creation consumes a lot of resources
3 multi-thread Service
Multi-threaded processing of Multi-client connection requests. Because threads share resources, you do not need to copy multiple resources as the process does, so processing is faster.
# Coding = utf-8from socket import * from threading import Threadfrom time import sleep # process client requests and execute def dealWithClient (newSocket, destAddr): while True: recvData = newSocket. recv (1024) if len (recvData)> 0: print ('recv [% s]: % s' % (str (destAddr), recvData) else: print ('[% s] The client has closed' % str (destAddr) break newSocket. 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: while True: print ('----- main process, wait for the arrival of the new client ------') newSocket, destAddr = serSocket. accept () print ('----- main process, and then create. new processes are responsible for processing data [% s] ----- 'client = Thread (target = dealWithClient, args = (newSocket, destAddr) client. start () # Do not close it here. threads share resources. Otherwise, all threads will be shut down # newSocket. close () finally: serSocket. close () if _ name _ = '_ main _': main ()