10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Module which supports allocation of ctypes objects from shared memory 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# multiprocessing/sharedctypes.py 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Copyright (c) 2006-2008, R Oudkerk 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# All rights reserved. 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Redistribution and use in source and binary forms, with or without 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# modification, are permitted provided that the following conditions 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# are met: 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1. Redistributions of source code must retain the above copyright 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# notice, this list of conditions and the following disclaimer. 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2. Redistributions in binary form must reproduce the above copyright 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# notice, this list of conditions and the following disclaimer in the 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# documentation and/or other materials provided with the distribution. 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 3. Neither the name of author nor the names of any contributors may be 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# used to endorse or promote products derived from this software 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# without specific prior written permission. 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# SUCH DAMAGE. 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport ctypes 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport weakref 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom multiprocessing import heap, RLock 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom multiprocessing.forking import assert_spawning, ForkingPickler 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = ['RawValue', 'RawArray', 'Value', 'Array', 'copy', 'synchronized'] 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotypecode_to_type = { 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'c': ctypes.c_char, 'u': ctypes.c_wchar, 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'b': ctypes.c_byte, 'B': ctypes.c_ubyte, 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'h': ctypes.c_short, 'H': ctypes.c_ushort, 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'i': ctypes.c_int, 'I': ctypes.c_uint, 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'l': ctypes.c_long, 'L': ctypes.c_ulong, 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'f': ctypes.c_float, 'd': ctypes.c_double 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao } 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _new_value(type_): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao size = ctypes.sizeof(type_) 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao wrapper = heap.BufferWrapper(size) 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return rebuild_ctype(type_, wrapper, None) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef RawValue(typecode_or_type, *args): 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Returns a ctypes object allocated from shared memory 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = _new_value(type_) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj.__init__(*args) 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef RawArray(typecode_or_type, size_or_initializer): 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Returns a ctypes array allocated from shared memory 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_ = typecode_to_type.get(typecode_or_type, typecode_or_type) 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(size_or_initializer, (int, long)): 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_ = type_ * size_or_initializer 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = _new_value(type_) 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj)) 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_ = type_ * len(size_or_initializer) 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = _new_value(type_) 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.__init__(*size_or_initializer) 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return result 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Value(typecode_or_type, *args, **kwds): 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a synchronization wrapper for a Value 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lock = kwds.pop('lock', None) 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if kwds: 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys()) 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = RawValue(typecode_or_type, *args) 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if lock is False: 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if lock in (True, None): 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lock = RLock() 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not hasattr(lock, 'acquire'): 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise AttributeError("'%r' has no method 'acquire'" % lock) 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return synchronized(obj, lock) 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Array(typecode_or_type, size_or_initializer, **kwds): 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return a synchronization wrapper for a RawArray 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ''' 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lock = kwds.pop('lock', None) 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if kwds: 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys()) 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = RawArray(typecode_or_type, size_or_initializer) 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if lock is False: 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if lock in (True, None): 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lock = RLock() 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not hasattr(lock, 'acquire'): 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise AttributeError("'%r' has no method 'acquire'" % lock) 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return synchronized(obj, lock) 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef copy(obj): 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao new_obj = _new_value(type(obj)) 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctypes.pointer(new_obj)[0] = obj 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return new_obj 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef synchronized(obj, lock=None): 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert not isinstance(obj, SynchronizedBase), 'object already synchronized' 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(obj, ctypes._SimpleCData): 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Synchronized(obj, lock) 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif isinstance(obj, ctypes.Array): 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if obj._type_ is ctypes.c_char: 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return SynchronizedString(obj, lock) 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return SynchronizedArray(obj, lock) 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cls = type(obj) 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao scls = class_cache[cls] 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError: 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao names = [field[0] for field in cls._fields_] 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict((name, make_property(name)) for name in names) 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao classname = 'Synchronized' + cls.__name__ 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao scls = class_cache[cls] = type(classname, (SynchronizedBase,), d) 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return scls(obj, lock) 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Functions for pickling/unpickling 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef reduce_ctype(obj): 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert_spawning(obj) 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(obj, ctypes.Array): 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return rebuild_ctype, (obj._type_, obj._wrapper, obj._length_) 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return rebuild_ctype, (type(obj), obj._wrapper, None) 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef rebuild_ctype(type_, wrapper, length): 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if length is not None: 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_ = type_ * length 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ForkingPickler.register(type_, reduce_ctype) 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = type_.from_address(wrapper.get_address()) 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj._wrapper = wrapper 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Function to create properties 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef make_property(name): 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return prop_cache[name] 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError: 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = {} 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exec template % ((name,)*7) in d 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prop_cache[name] = d[name] 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return d[name] 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotemplate = ''' 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef get%s(self): 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._obj.%s 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef set%s(self, value): 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._obj.%s = value 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao%s = property(get%s, set%s) 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao''' 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoprop_cache = {} 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass_cache = weakref.WeakKeyDictionary() 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Synchronized wrappers 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SynchronizedBase(object): 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, obj, lock=None): 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._obj = obj 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._lock = lock or RLock() 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire = self._lock.acquire 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release = self._lock.release 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao assert_spawning(self) 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return synchronized, (self._obj, self._lock) 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_obj(self): 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._obj 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def get_lock(self): 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._lock 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '<%s wrapper for %s>' % (type(self).__name__, self._obj) 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Synchronized(SynchronizedBase): 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value = make_property('value') 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SynchronizedArray(SynchronizedBase): 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __len__(self): 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(self._obj) 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getitem__(self, i): 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._obj[i] 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setitem__(self, i, value): 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._obj[i] = value 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getslice__(self, start, stop): 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._obj[start:stop] 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setslice__(self, start, stop, values): 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.acquire() 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._obj[start:stop] = values 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.release() 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SynchronizedString(SynchronizedArray): 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value = make_property('value') 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raw = make_property('raw') 267