Can socket objects be shared with Python's multiprocessing? socket.close() does not seem to be working
Question: I'm writing a server which uses multiprocessing.Process for each client.
socket.accept() is being called in a parent process and the connection object
is given as an argument to the Process.

The problem is that when calling socket.close() the socket does not seem to be
closing. The client's recv() should return immediately after close() has been
called on the server. This is the case when using threading.Thread or just
handle the requests in the main thread, however when using multiprocessing,
the client's recv seem to be hanging forever.

[Some sources](http://stackoverflow.com/a/8686107/1132184) indicate that
socket objects should be shared as handles with multiprocessing.Pipes and
multiprocess.reduction but it does not seem to make a difference.

EDIT: I am using Python 2.7.4 on Linux 64 bit .

Below are the sample implementation demonstrating this issue.

# server.py

    
    
    import socket
    from multiprocessing import Process
    #from threading import Thread as Process
    
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('', 5001))
    s.listen(5)
    
    def process(s):
        print "accepted"
        s.close()
        print "closed"
    
    while True:
        print "accepting"
        c, _ = s.accept()
        p = Process(target=process, args=(c,))
        p.start()
        print "started process"
    

# client.py

    
    
    import socket
    
    s = socket.socket()
    s.connect(('', 5001))
    print "connected"
    buf = s.recv(1024)
    
    print "buf: '" + buf +"'"
    
    s.close()
    


Answer: The problem is that the socket is not closed in the parent process. Therefore
it remains open, and causes the symptom you are observing.

Immediately after forking off the child process to handle the connection, you
should close the parent process' copy of the socket, like so:

    
    
    while True:
        print "accepting"
        c, _ = s.accept()
        p = Process(target=process, args=(c,))
        p.start()
        print "started process"
        c.close()
    

