Process pool | thread pool (synchronous, asynchronous blocking, non-blocking)
multi-threading: IO-intensive
multi-process: Compute-intensive
thread is a cpu to run the unit, the process is resource unit
from socket import * from threading import Thread def comunicat(conn): while True: # 通信循环 try: data = conn.recv(1024) if len(data) == 0: break conn.send(data.upper()) except ConnectionResetError: break conn.close() def server(ip, port, backlog=5): server = socket(AF_INET, SOCK_STREAM) server.bind((ip,port)) server.listen(backlog) while True: # 链接循环 conn, client_addr = server.accept() print(client_addr) #通信 t=Thread(target=comunicat,args=(conn,)) t.start() if __name__ == '__main__': s=Thread(target=server,args=('127.0.0.1',8081)) s.start()
from socket import * client=socket(AF_INET,SOCK_STREAM) client.connect(('127.0.0.1',8081)) while True: msg=input(">>:").strip() if len(msg) == 0: continue client.send(msg.encode('utf-8')) data=client.recv(1024) print(data.decode('utf-8'))
Achieved when the server is turned on, you can run multiple clients, but there are ten thousand client, the server will open multiple threads to run, this will be a waste of resources