I am currently reading Python 2.7's multiprocessing code. I am confused about the implementation of connection.Pipe.
It uses the following code to create a duplex pipe.
s1, s2 = socket.socketpair()
s1.setblocking(True)
s2.setblocking(True)
c1 = _multiprocessing.Connection(os.dup(s1.fileno()))
c2 = _multiprocessing.Connection(os.dup(s2.fileno()))
s1.close()
s2.close()
But why should we firstly dup
s1
and s2
's fileno and close them instead of using them directly?
I think it is quite different from the following implementation in C which close only parent on child process(and child on parent process) after fork
is called.