[issue11119] Passing a socket to a process (multiprocessing module)

Antoine Pitrou report at bugs.python.org
Sat Feb 5 09:25:07 CET 2011


Antoine Pitrou <pitrou at free.fr> added the comment:

Well, sockets cannot be pickled on any platform:

>>> sock = socket.create_connection(("www.python.org", 80))
__main__:1: ResourceWarning: unclosed <socket.socket object, fd=3, family=2, type=1, proto=0>
>>> s = pickle.loads(pickle.dumps(sock))
>>> s.getpeername()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: getsockaddrlen: bad family
>>> s.fileno()
-1

The reason your code works under Linux is that multiprocessing uses fork() and therefore all objects and file handles are transparently inherited by the child. Windows doesn't have fork(), it instead spawns a new process to which it must marshal objects using pickle. You'll have to create your socket in the child for it to work at all.

By the way, multi-threading is much more appropriate than multi-processing when writing servers under Windows. Also, see the socketserver module for helpers to write both multi-threaded and multi-processed servers: http://docs.python.org/library/socketserver.html

----------
nosy: +pitrou
resolution:  -> invalid
status: open -> pending

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11119>
_______________________________________


More information about the Python-bugs-list mailing list