Can socket objects be shared with Python's multiprocessing? socket.close() does not seem to be working

大兔子大兔子 提交于 2020-01-22 15:30:25

问题


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 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()

回答1:


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()


来源:https://stackoverflow.com/questions/17297810/can-socket-objects-be-shared-with-pythons-multiprocessing-socket-close-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!