7.18 进程池方式的 socket
2018-7-18 22:05:28 笔记都做完了 睡觉睡觉!!!明天早起!发现python慢慢深入变得很强大,很好玩!!!
进程池版本的socket 更加优化了,比多进程版本的效率更高
seerver端
# !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 20:50 # !@Author TrueNewBee # 用进程池子方法让服务端接就收多个客户端发来消息 # apply_async() 异步方法 import socket from multiprocessing import Pool def func(conn1): conn1.send(b'hello') print(conn.recv(1024).decode('utf-8')) conn1.close() if __name__ == '__main__': p = Pool(5) sk = socket.socket() sk.bind(('127.0.0.1', 8080)) sk.listen() while True: # 需要异步 多进程就收客户端发来消息 conn, add = sk.accept() p.apply_async(func, args=(conn, )) sk.close()
client 端
# !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 20:50 # !@Author TrueNewBee import socket sk = socket.socket() sk.connect(('127.0.0.1', 8080)) ret = sk.recv(1024).decode('utf-8') print(ret) msg = input('>>>').encode('utf-8') sk.send(msg) sk.close()