python3 socket.socketpair () for interprocess communication
socket . socketpair () What the hell is that?
socket.socketpair () function returns only two have been connected socket object, and parameters socket.socket () parameters within the same usage.
socket.socketpair () can be understood as the creation of two sockets, likened to a server socket, a client socket, these two sockets are already connected
socket.socketpair () is a full-duplex mode, that is, each socket can be sent and received, analogous to server.send ---> client.recv , and client.send ---> s erver . recv
socket.socketpair () defaults to creating unix sockets
Use examples to understand:
1. Socket.socketpair in a single process
a,b=socket.socketpair() print(a) print(b) a.send(b'hello') brecv=b.recv(1024) print('b recv',brecv.decode()) b.send ( ' Hello ' .encode ()) arecv=a.recv(1024) print('a recv',arecv.decode())
result:
<socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> b recv hello a recv hello
2. socket.socketpair in multiple processes os.fork ()
os.fork参考 url :https://www.cnblogs.com/lijinlei521/p/12699388.html
os.fork is a clone process. Create a child process in the main process, and copy all the memory addresses of the main process to the child process, and execute the code block after the fork together. The parent and child processes will return different pid numbers to distinguish the main process ( The main process returns the child process pid) or the child process (the child process returns pid number 0)
import os import socket socket1,socket2=socket.socketpair() pid=os.fork() if pid == 0: print ( ' child process ' ) print ( ' child process- ' , socket1, id (socket1)) print ( ' child process- ' , socket2, id (socket2)) else : print ( ' parent process ' ) print ( ' parent process- ' , socket1, id (socket1)) print ( ' parent process- ' , socket2, id (socket2))
result:
Parent process 父进程- <socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> 4334200680 父进程- <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> 4334200776 Child process 子进程- <socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> 4334200680 子进程- <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> 4334200776
Description:
1.os.fork () copies all the memory addresses of the main process to the child process, so the child process and the parent process now have two sockets (socket1, socket2) with the same (the same id result)
2. So you can use one in each process, turn off the other useless, just leave a socket for your own use, the other uses
import os import socket socket1,socket2=socket.socketpair() pid=os.fork() if pid == 0: print ( ' child process ' ) socket1.close () socket2.send ( ' send to the other parent process ' .encode ()) else : print ( ' parent process ' ) socket2.close () recv = socket1.recv (1024 ) print ( ' parent process received: ' , recv.decode ())
Parent process Child process Parent process received: sent to the other parent process
3. The principle of socket.socketpair in multiprocessing multiprocessing is the same as os.fork ()
import socket from multiprocessing import Process socket1,socket2=socket.socketpair() def test(): print(socket1) print(socket2) p1=Process(target=test) p2=Process(target=test) p1.start() p2.start()
<socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0>
import socket from multiprocessing import Process socket1,socket2=socket.socketpair() def test (): print (socket1) print (socket2) # socket2.close () #may not be closed, but it does not make sense, unless in a single process, socket1 and socket2 play with their own hair, and then print ( ' socket1 send to socket2 hello ' ) socket1.send(b'hello') def test2(): print(socket1) print(socket2) # socket1.close() recv=socket2.recv(1024) print('test2 recv',recv.decode()) p1=Process(target=test) p2=Process(target=test2) p1.start() p2.start()
<socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> socket1 send to socket2 hello <socket.socket fd=4, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> <socket.socket fd=5, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0> test2 recv hello
4. Multi-threaded Thearding in socket.socketpair with socket.socketpair single process principles
1. Multi-threading is executed in a process, there is no concept of copying process, so there is only one pair of socket pairs, so you can not close any socket, if closed, the only pair of connections can be broken, you can not send and receive work Off
2. Multi-process that may close because the child process to copy and replicate a pair of socketpari () , just create a shortcut way , is like delete delete a shortcut, the file still, if the file is also deleted really Closed, no communication
import socket from threading import Thread socket1,socket2=socket.socketpair() def test(): print('socket1 send to socket2 hello') socket1.send(b'hello') def test2(): recv=socket2.recv(1024) print('test2 recv',recv.decode()) p1=Thread(target=test) p2=Thread(target=test2) p1.start() p2.start()
socket1 send to socket2 hello test2 recv hello