pool pool implementation process socket concurrent connections

Server:

# ### 进程池实现socket连接服务器

from multiprocessing import Pool
import socket,os

sk = socket.socket()
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)

sk.bind(("127.0.0.1",8080))

sk.listen()

def talk(conn,addr):
    while True:
        msg = conn.recv(1024).decode("utf-8")
        if msg == "q":
            break
        print("{} {}>>>:{}".format(os.getpid(),addr[1],msg))
    conn.close()
    

if __name__ == "__main__":
    p = Pool(5)
    while True:
        conn,addr = sk.accept()
        p.apply_async(talk,args=(conn,addr))

    p.close()
    p.join()
    sk.close()

Client:

# ### socket connection pool implementation process server 

Import socket 

SK = socket.socket () 

sk.connect (( " 127.0.0.1 " , 8080 )) 

the while True: 
    msg = the INPUT ( " Client >>>: " ) 
    SK .send (msg.encode ( " UTF-. 8 " ))
     IF MSG == " Q " :
         BREAK 
sk.close ()

 

Guess you like

Origin www.cnblogs.com/weige007/p/11870980.html