Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Module to allow connection and socket objects to be transferred |
| 3 | # between processes |
| 4 | # |
| 5 | # multiprocessing/reduction.py |
| 6 | # |
R. David Murray | 3fc969a | 2010-12-14 01:38:16 +0000 | [diff] [blame] | 7 | # Copyright (c) 2006-2008, R Oudkerk |
| 8 | # All rights reserved. |
| 9 | # |
| 10 | # Redistribution and use in source and binary forms, with or without |
| 11 | # modification, are permitted provided that the following conditions |
| 12 | # are met: |
| 13 | # |
| 14 | # 1. Redistributions of source code must retain the above copyright |
| 15 | # notice, this list of conditions and the following disclaimer. |
| 16 | # 2. Redistributions in binary form must reproduce the above copyright |
| 17 | # notice, this list of conditions and the following disclaimer in the |
| 18 | # documentation and/or other materials provided with the distribution. |
| 19 | # 3. Neither the name of author nor the names of any contributors may be |
| 20 | # used to endorse or promote products derived from this software |
| 21 | # without specific prior written permission. |
| 22 | # |
| 23 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
| 24 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 27 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | # SUCH DAMAGE. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 34 | # |
| 35 | |
| 36 | __all__ = [] |
| 37 | |
| 38 | import os |
| 39 | import sys |
| 40 | import socket |
| 41 | import threading |
Charles-François Natali | dc863dd | 2011-09-24 20:04:29 +0200 | [diff] [blame] | 42 | import struct |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 43 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 44 | from multiprocessing import current_process |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 45 | from multiprocessing.forking import Popen, duplicate, close, ForkingPickler |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 46 | from multiprocessing.util import register_after_fork, debug, sub_debug |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 47 | from multiprocessing.connection import Client, Listener, Connection |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | # |
| 51 | # |
| 52 | # |
| 53 | |
Charles-François Natali | dc863dd | 2011-09-24 20:04:29 +0200 | [diff] [blame] | 54 | if not(sys.platform == 'win32' or (hasattr(socket, 'CMSG_LEN') and |
| 55 | hasattr(socket, 'SCM_RIGHTS'))): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 56 | raise ImportError('pickling of connections not supported') |
| 57 | |
| 58 | # |
| 59 | # Platform specific definitions |
| 60 | # |
| 61 | |
| 62 | if sys.platform == 'win32': |
Brian Curtin | a6a3274 | 2010-08-04 15:47:24 +0000 | [diff] [blame] | 63 | from _multiprocessing import win32 |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 64 | |
| 65 | def send_handle(conn, handle, destination_pid): |
| 66 | process_handle = win32.OpenProcess( |
| 67 | win32.PROCESS_ALL_ACCESS, False, destination_pid |
| 68 | ) |
| 69 | try: |
| 70 | new_handle = duplicate(handle, process_handle) |
| 71 | conn.send(new_handle) |
| 72 | finally: |
| 73 | close(process_handle) |
| 74 | |
| 75 | def recv_handle(conn): |
| 76 | return conn.recv() |
| 77 | |
| 78 | else: |
| 79 | def send_handle(conn, handle, destination_pid): |
Charles-François Natali | dc863dd | 2011-09-24 20:04:29 +0200 | [diff] [blame] | 80 | with socket.fromfd(conn.fileno(), socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 81 | s.sendmsg([b'x'], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, |
| 82 | struct.pack("@i", handle))]) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 83 | |
| 84 | def recv_handle(conn): |
Charles-François Natali | dc863dd | 2011-09-24 20:04:29 +0200 | [diff] [blame] | 85 | size = struct.calcsize("@i") |
| 86 | with socket.fromfd(conn.fileno(), socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 87 | msg, ancdata, flags, addr = s.recvmsg(1, socket.CMSG_LEN(size)) |
| 88 | try: |
| 89 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 90 | if (cmsg_level == socket.SOL_SOCKET and |
| 91 | cmsg_type == socket.SCM_RIGHTS): |
| 92 | return struct.unpack("@i", cmsg_data[:size])[0] |
| 93 | except (ValueError, IndexError, struct.error): |
| 94 | pass |
| 95 | raise RuntimeError('Invalid data received') |
| 96 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 97 | |
| 98 | # |
| 99 | # Support for a per-process server thread which caches pickled handles |
| 100 | # |
| 101 | |
| 102 | _cache = set() |
| 103 | |
| 104 | def _reset(obj): |
| 105 | global _lock, _listener, _cache |
| 106 | for h in _cache: |
| 107 | close(h) |
| 108 | _cache.clear() |
| 109 | _lock = threading.Lock() |
| 110 | _listener = None |
| 111 | |
| 112 | _reset(None) |
| 113 | register_after_fork(_reset, _reset) |
| 114 | |
| 115 | def _get_listener(): |
| 116 | global _listener |
| 117 | |
| 118 | if _listener is None: |
| 119 | _lock.acquire() |
| 120 | try: |
| 121 | if _listener is None: |
| 122 | debug('starting listener and thread for sending handles') |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 123 | _listener = Listener(authkey=current_process().authkey) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 124 | t = threading.Thread(target=_serve) |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 125 | t.daemon = True |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 126 | t.start() |
| 127 | finally: |
| 128 | _lock.release() |
| 129 | |
| 130 | return _listener |
| 131 | |
| 132 | def _serve(): |
| 133 | from .util import is_exiting, sub_warning |
| 134 | |
| 135 | while 1: |
| 136 | try: |
| 137 | conn = _listener.accept() |
| 138 | handle_wanted, destination_pid = conn.recv() |
| 139 | _cache.remove(handle_wanted) |
| 140 | send_handle(conn, handle_wanted, destination_pid) |
| 141 | close(handle_wanted) |
| 142 | conn.close() |
| 143 | except: |
| 144 | if not is_exiting(): |
| 145 | import traceback |
| 146 | sub_warning( |
| 147 | 'thread for sharing handles raised exception :\n' + |
| 148 | '-'*79 + '\n' + traceback.format_exc() + '-'*79 |
| 149 | ) |
| 150 | |
| 151 | # |
| 152 | # Functions to be used for pickling/unpickling objects with handles |
| 153 | # |
| 154 | |
| 155 | def reduce_handle(handle): |
| 156 | if Popen.thread_is_spawning(): |
| 157 | return (None, Popen.duplicate_for_child(handle), True) |
| 158 | dup_handle = duplicate(handle) |
| 159 | _cache.add(dup_handle) |
| 160 | sub_debug('reducing handle %d', handle) |
| 161 | return (_get_listener().address, dup_handle, False) |
| 162 | |
| 163 | def rebuild_handle(pickled_data): |
| 164 | address, handle, inherited = pickled_data |
| 165 | if inherited: |
| 166 | return handle |
| 167 | sub_debug('rebuilding handle %d', handle) |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 168 | conn = Client(address, authkey=current_process().authkey) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 169 | conn.send((handle, os.getpid())) |
| 170 | new_handle = recv_handle(conn) |
| 171 | conn.close() |
| 172 | return new_handle |
| 173 | |
| 174 | # |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 175 | # Register `Connection` with `ForkingPickler` |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 176 | # |
| 177 | |
| 178 | def reduce_connection(conn): |
| 179 | rh = reduce_handle(conn.fileno()) |
| 180 | return rebuild_connection, (rh, conn.readable, conn.writable) |
| 181 | |
| 182 | def rebuild_connection(reduced_handle, readable, writable): |
| 183 | handle = rebuild_handle(reduced_handle) |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 184 | return Connection( |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 185 | handle, readable=readable, writable=writable |
| 186 | ) |
| 187 | |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 188 | ForkingPickler.register(Connection, reduce_connection) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 189 | |
| 190 | # |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 191 | # Register `socket.socket` with `ForkingPickler` |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 192 | # |
| 193 | |
| 194 | def fromfd(fd, family, type_, proto=0): |
| 195 | s = socket.fromfd(fd, family, type_, proto) |
| 196 | if s.__class__ is not socket.socket: |
| 197 | s = socket.socket(_sock=s) |
| 198 | return s |
| 199 | |
| 200 | def reduce_socket(s): |
| 201 | reduced_handle = reduce_handle(s.fileno()) |
| 202 | return rebuild_socket, (reduced_handle, s.family, s.type, s.proto) |
| 203 | |
| 204 | def rebuild_socket(reduced_handle, family, type_, proto): |
| 205 | fd = rebuild_handle(reduced_handle) |
| 206 | _sock = fromfd(fd, family, type_, proto) |
| 207 | close(fd) |
| 208 | return _sock |
| 209 | |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 210 | ForkingPickler.register(socket.socket, reduce_socket) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 211 | |
| 212 | # |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 213 | # Register `_multiprocessing.PipeConnection` with `ForkingPickler` |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 214 | # |
| 215 | |
| 216 | if sys.platform == 'win32': |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 217 | from multiprocessing.connection import PipeConnection |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 218 | |
| 219 | def reduce_pipe_connection(conn): |
| 220 | rh = reduce_handle(conn.fileno()) |
| 221 | return rebuild_pipe_connection, (rh, conn.readable, conn.writable) |
| 222 | |
| 223 | def rebuild_pipe_connection(reduced_handle, readable, writable): |
| 224 | handle = rebuild_handle(reduced_handle) |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 225 | return PipeConnection( |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 226 | handle, readable=readable, writable=writable |
| 227 | ) |
| 228 | |
Antoine Pitrou | 87cf220 | 2011-05-09 17:04:27 +0200 | [diff] [blame] | 229 | ForkingPickler.register(PipeConnection, reduce_pipe_connection) |