def __init__(self,line='',task=None,start=False,process=False,keep=10,trace=False):
if line: self.load(line)
if task is not None: self.task = task
self.last_match = 0
self.trace = trace
self.keep = keep
self.THREAD_CLASS = threading.Thread if not process else multiprocessing.Process
self.QUEUE_CLASS = Queue.Queue if not process else multiprocessing.Queue
self.EVENT_CLASS = threading.Event if not process else multiprocessing.Event
self.LOCK_CLASS = threading.RLock if not process else multiprocessing.RLock
self._thread = None
self.event = None
self._queue = self.QUEUE_CLASS(maxsize=int(self.keep or 10))
if start: self.start()
python类RLock()的实例源码
def __init__(self,name='',process=False,wait=.01,target=None,hook=None,trace=False):
self._name = name
self.wait = wait
self._process = process
self._trace = trace
self.hook=hook
self.THREAD_CLASS = threading.Thread if not process else multiprocessing.Process
self.QUEUE_CLASS = Queue.Queue if not process else multiprocessing.Queue
self.EVENT_CLASS = threading.Event if not process else multiprocessing.Event
self.LOCK_CLASS = threading.RLock if not process else multiprocessing.RLock
self.inQueue = self.QUEUE_CLASS()
self.outQueue = self.QUEUE_CLASS()
self.errorQueue = self.QUEUE_CLASS()
self.stopEvent = self.EVENT_CLASS()
if target is not None:
self.put(target)
self._thread = self.THREAD_CLASS(name='Worker',target=self.run)
self._thread.daemon = True
pass
def __init__(self, shape, dtype = numpy.float32):
num_elems = numpy.prod(shape)
if dtype == numpy.int32:
c_type = ctypes.c_int
elif dtype == numpy.float32:
c_type = ctypes.c_float
elif dtype == numpy.float64:
c_type = ctypes.c_double
else:
assert(0)
#shared storage for numpy array
self.shape = shape
self.dtype = dtype
self.base = mp.RawArray(c_type, int(num_elems))
self.lock = mp.RLock()
#overloaded operators for convienince
def __init__(self):
self.df_to_tp = dict()
self.attached_dataframes = set()
self.tp_to_attached_df = dict()
self.queues = dict()
self.type_map = dict()
self.add_lock = RLock()
#################################################
### Static Methods ##############################
#################################################
#################################################
### API Methods #################################
#################################################
def Value(typecode_or_type, *args, **kwds):
'''
Return a synchronization wrapper for a Value
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def Value(typecode_or_type, *args, **kwds):
'''
Return a synchronization wrapper for a Value
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def __init__(self, base, surfix = '', freq = "daily", cacheline = 200, flushnow = 0):
self.base = base
self.surfix = surfix
self.freq = freq
pathtool.mkdir (base)
self.file = "%s/%s.log" % (self.base, self.surfix)
base_logger.__init__ (self, codecs.open (self.file, "a", "utf8"), cacheline, flushnow)
self.cv = multiprocessing.Condition (multiprocessing.RLock())
self.using = 0
self.numlog = 0
self.maintern ()
self.rotate_when = self.get_next_rotate (self.freq)
def __init__(self, resolver=None, host: str = "nonhost") -> None:
self._hostResolvers = [resolver] if resolver is not None else []
# python dict structure is atomic for primitive actions. Need to be checked
self.__local_actor_refs = {}
self.__sequence_id = 0
self.__address = host
self.__lock = RLock()
def singleton(cls):
"""Decorator to create singleton classes"""
instances = {}
lock = RLock()
def get_instance(*args, **kwargs):
if cls not in instances:
with lock:
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
def Value(typecode_or_type, *args, lock=None):
'''
Return a synchronization wrapper for a Value
'''
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % list(kwds.keys()))
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def __init__(self, directory, **kwargs):
"""
:param directory: root dir for storage
"""
super(FileSystemResourceAPI, self).__init__(**kwargs)
self.directory = directory
self.base_path = os.path.join(self.directory, self.name)
self._join_path = partial(os.path.join, self.base_path)
self._lock = RLock()
def __init__(self, path):
self.path = path
self._lock = multiprocessing.RLock()
self._create_path(self.path)
def Value(typecode_or_type, *args, **kwds):
'''
Return a synchronization wrapper for a Value
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def test_rlock(self):
lock = self.RLock()
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.release(), None)
self.assertEqual(lock.release(), None)
self.assertEqual(lock.release(), None)
self.assertRaises((AssertionError, RuntimeError), lock.release)
def Value(typecode_or_type, *args, **kwds):
'''
Return a synchronization wrapper for a Value
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def test_rlock(self):
lock = self.RLock()
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.acquire(), True)
self.assertEqual(lock.release(), None)
self.assertEqual(lock.release(), None)
self.assertEqual(lock.release(), None)
self.assertRaises((AssertionError, RuntimeError), lock.release)
def Value(typecode_or_type, *args, **kwds):
'''
Return a synchronization wrapper for a Value
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawValue(typecode_or_type, *args)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def Array(typecode_or_type, size_or_initializer, **kwds):
'''
Return a synchronization wrapper for a RawArray
'''
lock = kwds.pop('lock', None)
if kwds:
raise ValueError('unrecognized keyword argument(s): %s' % kwds.keys())
obj = RawArray(typecode_or_type, size_or_initializer)
if lock is False:
return obj
if lock in (True, None):
lock = RLock()
if not hasattr(lock, 'acquire'):
raise AttributeError("'%r' has no method 'acquire'" % lock)
return synchronized(obj, lock)
def __init__(self, obj, lock=None):
self._obj = obj
self._lock = lock or RLock()
self.acquire = self._lock.acquire
self.release = self._lock.release
def __init__(self, value=0):
self.value = value
self.lock = RLock()