使用多进程 实现socket tcp协议 server端的并发
server端:
from multiprocessing import Process import socket def func(conn): while 1: conn.send(b'hello,this is server') if __name__ == '__main__': # 子进程会导入这句话上面的代码 sk = socket.socket() sk.bind(('127.0.0.1', 8888)) sk.listen() while 1: conn,addr = sk.accept() # 接收到连接交给子进程去执行,然后循环等待下一个连接 Process(target=func, args=(conn,)).start()
client端
import socket sk = socket.socket() sk.connect(('127.0.0.1',8888)) while 1: msg = sk.recv(1024) print(msg)
对比图: