| 1 | n/a | # |
|---|
| 2 | n/a | # Module which supports allocation of ctypes objects from shared memory |
|---|
| 3 | n/a | # |
|---|
| 4 | n/a | # multiprocessing/sharedctypes.py |
|---|
| 5 | n/a | # |
|---|
| 6 | n/a | # Copyright (c) 2006-2008, R Oudkerk |
|---|
| 7 | n/a | # Licensed to PSF under a Contributor Agreement. |
|---|
| 8 | n/a | # |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | import ctypes |
|---|
| 11 | n/a | import weakref |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | from . import heap |
|---|
| 14 | n/a | from . import get_context |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | from .context import reduction, assert_spawning |
|---|
| 17 | n/a | _ForkingPickler = reduction.ForkingPickler |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | __all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized'] |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | # |
|---|
| 22 | n/a | # |
|---|
| 23 | n/a | # |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | typecode_to_type = { |
|---|
| 26 | n/a | 'c': ctypes.c_char, 'u': ctypes.c_wchar, |
|---|
| 27 | n/a | 'b': ctypes.c_byte, 'B': ctypes.c_ubyte, |
|---|
| 28 | n/a | 'h': ctypes.c_short, 'H': ctypes.c_ushort, |
|---|
| 29 | n/a | 'i': ctypes.c_int, 'I': ctypes.c_uint, |
|---|
| 30 | n/a | 'l': ctypes.c_long, 'L': ctypes.c_ulong, |
|---|
| 31 | n/a | 'f': ctypes.c_float, 'd': ctypes.c_double |
|---|
| 32 | n/a | } |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | # |
|---|
| 35 | n/a | # |
|---|
| 36 | n/a | # |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def _new_value(type_): |
|---|
| 39 | n/a | size = ctypes.sizeof(type_) |
|---|
| 40 | n/a | wrapper = heap.BufferWrapper(size) |
|---|
| 41 | n/a | return rebuild_ctype(type_, wrapper, None) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def RawValue(typecode_or_type, *args): |
|---|
| 44 | n/a | ''' |
|---|
| 45 | n/a | Returns a ctypes object allocated from shared memory |
|---|
| 46 | n/a | ''' |
|---|
| 47 | n/a | type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) |
|---|
| 48 | n/a | obj = _new_value(type_) |
|---|
| 49 | n/a | ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) |
|---|
| 50 | n/a | obj.__init__(*args) |
|---|
| 51 | n/a | return obj |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | def RawArray(typecode_or_type, size_or_initializer): |
|---|
| 54 | n/a | ''' |
|---|
| 55 | n/a | Returns a ctypes array allocated from shared memory |
|---|
| 56 | n/a | ''' |
|---|
| 57 | n/a | type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) |
|---|
| 58 | n/a | if isinstance(size_or_initializer, int): |
|---|
| 59 | n/a | type_ = type_ * size_or_initializer |
|---|
| 60 | n/a | obj = _new_value(type_) |
|---|
| 61 | n/a | ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) |
|---|
| 62 | n/a | return obj |
|---|
| 63 | n/a | else: |
|---|
| 64 | n/a | type_ = type_ * len(size_or_initializer) |
|---|
| 65 | n/a | result = _new_value(type_) |
|---|
| 66 | n/a | result.__init__(*size_or_initializer) |
|---|
| 67 | n/a | return result |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def Value(typecode_or_type, *args, lock=True, ctx=None): |
|---|
| 70 | n/a | ''' |
|---|
| 71 | n/a | Return a synchronization wrapper for a Value |
|---|
| 72 | n/a | ''' |
|---|
| 73 | n/a | obj = RawValue(typecode_or_type, *args) |
|---|
| 74 | n/a | if lock is False: |
|---|
| 75 | n/a | return obj |
|---|
| 76 | n/a | if lock in (True, None): |
|---|
| 77 | n/a | ctx = ctx or get_context() |
|---|
| 78 | n/a | lock = ctx.RLock() |
|---|
| 79 | n/a | if not hasattr(lock, 'acquire'): |
|---|
| 80 | n/a | raise AttributeError("'%r' has no method 'acquire'" % lock) |
|---|
| 81 | n/a | return synchronized(obj, lock, ctx=ctx) |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def Array(typecode_or_type, size_or_initializer, *, lock=True, ctx=None): |
|---|
| 84 | n/a | ''' |
|---|
| 85 | n/a | Return a synchronization wrapper for a RawArray |
|---|
| 86 | n/a | ''' |
|---|
| 87 | n/a | obj = RawArray(typecode_or_type, size_or_initializer) |
|---|
| 88 | n/a | if lock is False: |
|---|
| 89 | n/a | return obj |
|---|
| 90 | n/a | if lock in (True, None): |
|---|
| 91 | n/a | ctx = ctx or get_context() |
|---|
| 92 | n/a | lock = ctx.RLock() |
|---|
| 93 | n/a | if not hasattr(lock, 'acquire'): |
|---|
| 94 | n/a | raise AttributeError("'%r' has no method 'acquire'" % lock) |
|---|
| 95 | n/a | return synchronized(obj, lock, ctx=ctx) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def copy(obj): |
|---|
| 98 | n/a | new_obj = _new_value(type(obj)) |
|---|
| 99 | n/a | ctypes.pointer(new_obj)[0] = obj |
|---|
| 100 | n/a | return new_obj |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def synchronized(obj, lock=None, ctx=None): |
|---|
| 103 | n/a | assert not isinstance(obj, SynchronizedBase), 'object already synchronized' |
|---|
| 104 | n/a | ctx = ctx or get_context() |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | if isinstance(obj, ctypes._SimpleCData): |
|---|
| 107 | n/a | return Synchronized(obj, lock, ctx) |
|---|
| 108 | n/a | elif isinstance(obj, ctypes.Array): |
|---|
| 109 | n/a | if obj._type_ is ctypes.c_char: |
|---|
| 110 | n/a | return SynchronizedString(obj, lock, ctx) |
|---|
| 111 | n/a | return SynchronizedArray(obj, lock, ctx) |
|---|
| 112 | n/a | else: |
|---|
| 113 | n/a | cls = type(obj) |
|---|
| 114 | n/a | try: |
|---|
| 115 | n/a | scls = class_cache[cls] |
|---|
| 116 | n/a | except KeyError: |
|---|
| 117 | n/a | names = [field[0] for field in cls._fields_] |
|---|
| 118 | n/a | d = dict((name, make_property(name)) for name in names) |
|---|
| 119 | n/a | classname = 'Synchronized' + cls.__name__ |
|---|
| 120 | n/a | scls = class_cache[cls] = type(classname, (SynchronizedBase,), d) |
|---|
| 121 | n/a | return scls(obj, lock, ctx) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | # |
|---|
| 124 | n/a | # Functions for pickling/unpickling |
|---|
| 125 | n/a | # |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | def reduce_ctype(obj): |
|---|
| 128 | n/a | assert_spawning(obj) |
|---|
| 129 | n/a | if isinstance(obj, ctypes.Array): |
|---|
| 130 | n/a | return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_) |
|---|
| 131 | n/a | else: |
|---|
| 132 | n/a | return rebuild_ctype, (type(obj), obj._wrapper, None) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def rebuild_ctype(type_, wrapper, length): |
|---|
| 135 | n/a | if length is not None: |
|---|
| 136 | n/a | type_ = type_ * length |
|---|
| 137 | n/a | _ForkingPickler.register(type_, reduce_ctype) |
|---|
| 138 | n/a | buf = wrapper.create_memoryview() |
|---|
| 139 | n/a | obj = type_.from_buffer(buf) |
|---|
| 140 | n/a | obj._wrapper = wrapper |
|---|
| 141 | n/a | return obj |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | # |
|---|
| 144 | n/a | # Function to create properties |
|---|
| 145 | n/a | # |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def make_property(name): |
|---|
| 148 | n/a | try: |
|---|
| 149 | n/a | return prop_cache[name] |
|---|
| 150 | n/a | except KeyError: |
|---|
| 151 | n/a | d = {} |
|---|
| 152 | n/a | exec(template % ((name,)*7), d) |
|---|
| 153 | n/a | prop_cache[name] = d[name] |
|---|
| 154 | n/a | return d[name] |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | template = ''' |
|---|
| 157 | n/a | def get%s(self): |
|---|
| 158 | n/a | self.acquire() |
|---|
| 159 | n/a | try: |
|---|
| 160 | n/a | return self._obj.%s |
|---|
| 161 | n/a | finally: |
|---|
| 162 | n/a | self.release() |
|---|
| 163 | n/a | def set%s(self, value): |
|---|
| 164 | n/a | self.acquire() |
|---|
| 165 | n/a | try: |
|---|
| 166 | n/a | self._obj.%s = value |
|---|
| 167 | n/a | finally: |
|---|
| 168 | n/a | self.release() |
|---|
| 169 | n/a | %s = property(get%s, set%s) |
|---|
| 170 | n/a | ''' |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | prop_cache = {} |
|---|
| 173 | n/a | class_cache = weakref.WeakKeyDictionary() |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | # |
|---|
| 176 | n/a | # Synchronized wrappers |
|---|
| 177 | n/a | # |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | class SynchronizedBase(object): |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def __init__(self, obj, lock=None, ctx=None): |
|---|
| 182 | n/a | self._obj = obj |
|---|
| 183 | n/a | if lock: |
|---|
| 184 | n/a | self._lock = lock |
|---|
| 185 | n/a | else: |
|---|
| 186 | n/a | ctx = ctx or get_context(force=True) |
|---|
| 187 | n/a | self._lock = ctx.RLock() |
|---|
| 188 | n/a | self.acquire = self._lock.acquire |
|---|
| 189 | n/a | self.release = self._lock.release |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def __enter__(self): |
|---|
| 192 | n/a | return self._lock.__enter__() |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def __exit__(self, *args): |
|---|
| 195 | n/a | return self._lock.__exit__(*args) |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | def __reduce__(self): |
|---|
| 198 | n/a | assert_spawning(self) |
|---|
| 199 | n/a | return synchronized, (self._obj, self._lock) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def get_obj(self): |
|---|
| 202 | n/a | return self._obj |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def get_lock(self): |
|---|
| 205 | n/a | return self._lock |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def __repr__(self): |
|---|
| 208 | n/a | return '<%s wrapper for %s>' % (type(self).__name__, self._obj) |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | class Synchronized(SynchronizedBase): |
|---|
| 212 | n/a | value = make_property('value') |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | class SynchronizedArray(SynchronizedBase): |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | def __len__(self): |
|---|
| 218 | n/a | return len(self._obj) |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | def __getitem__(self, i): |
|---|
| 221 | n/a | with self: |
|---|
| 222 | n/a | return self._obj[i] |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | def __setitem__(self, i, value): |
|---|
| 225 | n/a | with self: |
|---|
| 226 | n/a | self._obj[i] = value |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | def __getslice__(self, start, stop): |
|---|
| 229 | n/a | with self: |
|---|
| 230 | n/a | return self._obj[start:stop] |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def __setslice__(self, start, stop, values): |
|---|
| 233 | n/a | with self: |
|---|
| 234 | n/a | self._obj[start:stop] = values |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | class SynchronizedString(SynchronizedArray): |
|---|
| 238 | n/a | value = make_property('value') |
|---|
| 239 | n/a | raw = make_property('raw') |
|---|