summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing/connection.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-04-24 20:56:57 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-04-24 20:56:57 (GMT)
commit5438ed1572f5e379cac8b85ed2226101a1bfcacc (patch)
treebf6a9df1be40d5d3df6fbfcdee606d5a49fca273 /Lib/multiprocessing/connection.py
parent9f478c021dc499b0d23ee418c0dcc6b5076524aa (diff)
downloadcpython-5438ed1572f5e379cac8b85ed2226101a1bfcacc.zip
cpython-5438ed1572f5e379cac8b85ed2226101a1bfcacc.tar.gz
cpython-5438ed1572f5e379cac8b85ed2226101a1bfcacc.tar.bz2
Issue #4892: multiprocessing Connections can now be transferred over multiprocessing Connections.
Patch by Richard Oudkerk (sbt).
Diffstat (limited to 'Lib/multiprocessing/connection.py')
-rw-r--r--Lib/multiprocessing/connection.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 3a61e5e..64d71bc 100644
--- 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()
- buf = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
- self._send_bytes(memoryview(buf))
+ buf = io.BytesIO()
+ ForkingPickler(buf, pickle.HIGHEST_PROTOCOL).dump(obj)
+ self._send_bytes(buf.getbuffer())
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':
+ from . import reduction
+ ForkingPickler.register(socket.socket, reduction.reduce_socket)
+ ForkingPickler.register(Connection, reduction.reduce_connection)
+ ForkingPickler.register(PipeConnection, reduction.reduce_pipe_connection)
+else:
+ try:
+ from . import reduction
+ except ImportError:
+ pass
+ else:
+ ForkingPickler.register(socket.socket, reduction.reduce_socket)
+ ForkingPickler.register(Connection, reduction.reduce_connection)