cpython: 08d4c2fe51ea (original) (raw)

--- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -832,6 +832,10 @@ Connection objects are usually created u raised and the complete message is available as e.args[0] where e is the exception instance.

+ For example:

--- a/Lib/multiprocessing/init.py +++ b/Lib/multiprocessing/init.py @@ -161,7 +161,9 @@ def allow_connection_pickling(): ''' Install support for sending connections and sockets between processes '''

#

Definitions depending on native semaphores

--- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -50,6 +50,7 @@ import _multiprocessing from multiprocessing import current_process, AuthenticationError, BufferTooShort from multiprocessing.util import ( get_temp_dir, Finalize, sub_debug, debug, _eintr_retry) +from multiprocessing.forking import ForkingPickler try: import _winapi from _winapi import WAIT_OBJECT_0, WAIT_TIMEOUT, INFINITE @@ -227,8 +228,9 @@ class _ConnectionBase: """Send a (picklable) object""" self._check_closed() self._check_writable()

def recv_bytes(self, maxlength=None): """ @@ -880,3 +882,21 @@ else: raise if timeout is not None: timeout = deadline - time.time() + +# +# Make connection and socket objects sharable if possible +# + +if sys.platform == 'win32':

+else:

--- a/Lib/multiprocessing/forking.py +++ b/Lib/multiprocessing/forking.py @@ -407,25 +407,6 @@ else: return d

-

-

-

- #

Prepare current process

#

--- a/Lib/multiprocessing/reduction.py +++ b/Lib/multiprocessing/reduction.py @@ -33,7 +33,7 @@

SUCH DAMAGE.

# -all = [] +all = ['reduce_socket', 'reduce_connection', 'send_handle', 'recv_handle'] import os import sys @@ -42,9 +42,8 @@ import threading import struct from multiprocessing import current_process -from multiprocessing.forking import Popen, duplicate, close, ForkingPickler from multiprocessing.util import register_after_fork, debug, sub_debug -from multiprocessing.connection import Client, Listener, Connection +from multiprocessing.util import is_exiting, sub_warning # @@ -60,22 +59,91 @@ if not(sys.platform == 'win32' or (hasat # if sys.platform == 'win32':

def recv_handle(conn):

+

+

+

+

+

+

+

+

+

+

else:

@@ -94,136 +162,109 @@ else: pass raise RuntimeError('Invalid data received')

+

+

+

+

+

# -# Support for a per-process server thread which caches pickled handles -# - -_cache = set() - -def _reset(obj):

- -_reset(None) -register_after_fork(_reset, _reset) - -def _get_listener():

-

-

- -def _serve():

-

- -# -# Functions to be used for pickling/unpickling objects with handles +# Server which shares registered resources with clients # -def reduce_handle(handle):

+class ResourceSharer(object):

-def rebuild_handle(pickled_data):

-# -# Register Connection with ForkingPickler -# - -def reduce_connection(conn):

- -def rebuild_connection(reduced_handle, readable, writable):

- -ForkingPickler.register(Connection, reduce_connection)

-# -# Register socket.socket with ForkingPickler -# - -def fromfd(fd, family, type_, proto=0):

- -def reduce_socket(s):

- -def rebuild_socket(reduced_handle, family, type_, proto):

-ForkingPickler.register(socket.socket, reduce_socket) - -# -# Register _multiprocessing.PipeConnection with ForkingPickler -# - -if sys.platform == 'win32':

-

+resource_sharer = ResourceSharer()

--- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -1959,49 +1959,49 @@ class _TestPoll(unittest.TestCase): #

Test of sending connection and socket objects between processes

# -""" + +@unittest.skipUnless(HAS_REDUCTION, "test needs multiprocessing.reduction") class _TestPicklingConnections(BaseTestCase): ALLOWED_TYPES = ('processes',)

-

+

conn.recv()

conn.close() def test_pickling(self):

- families = self.connection.families lconn, lconn0 = self.Pipe() @@ -2025,16 +2025,12 @@ class _TestPicklingConnections(BaseTestC rconn.send(None)

lconn.send(None) @@ -2043,7 +2039,46 @@ class _TestPicklingConnections(BaseTestC lp.join() rp.join() -""" +

+

+

+

+

+

+ # # #

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -71,6 +71,9 @@ Core and Builtins Library ------- +- Issue #4892: multiprocessing Connections can now be transferred over

--- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -1280,6 +1280,7 @@ PyInit__winapi(void) WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS);