多线程之间的通信和多进程之间通信原理是差不多的,所以在此直接上代码了,感兴趣的可以运行一下代码看看.
多进程代码块:
from socket import * from multiprocessing import * from time import sleep def dealWithClient(newSocket,destAddr): while True: recvData=newSocket.recv(1024).decode('gbk') if len(recvData)>0: print('来自%s的数据为:%s'%(str(destAddr),recvData)) newSocket.send(b'thanks!') else: print('[%s]客户端已经关闭'%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(2) try: while True: print('-----主进程,,等待新客户端的到来------') newSocket,destAddr=serSocket.accept() print('-----主进程,,接下来创建一个新的进程负责数据处理[%s]-----'%str(destAddr)) client=Process(target=dealWithClient,args=(newSocket,destAddr)) client.start() newSocket.close() finally: serSocket.close() if __name__=='__main__': main()
多线程代码块:
from socket import * from threading import Thread from time import sleep # 处理客户端的请求并执行事情 def dealWithClient(newSocket,destAddr): while True: recvData = newSocket.recv(1024).decode('gbk') if len(recvData)>0: print('recv[%s]:%s'%(str(destAddr), recvData)) newSocket.send(b'thread Ok!') else: print('[%s]客户端已经关闭'%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('-----主线程,,等待新客户端的到来------') newSocket,destAddr = serSocket.accept() print('-----主线程,,接下来创建一个新的线程负责数据处理[%s]-----'%str(destAddr)) client = Thread(target=dealWithClient, args=(newSocket,destAddr)) client.start() #因为线程中共享这个套接字,如果关闭了会导致这个套接字不可用, #但是此时在线程中这个套接字可能还在收数据,因此不能关闭 # newSocket.close() finally: serSocket.close() if __name__ == '__main__': main()