本文整理汇总了Python中atomic.Atomic类的典型用法代码示例。如果您正苦于以下问题:Python Atomic类的具体用法?Python Atomic怎么用?Python Atomic使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Atomic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_complex_update
def test_complex_update(self):
def complex_update(v):
return [v + 1 for v in v]
atomic = Atomic([-1, 0])
value = atomic.update(complex_update)
self.assertEqual([0, 1], atomic.value)
self.assertEqual([0, 1], value)
示例2: __init__
def __init__(self, sample):
self.sample = sample
self.counter = Atomic(0)
self.minimum = Atomic()
self.maximum = Atomic()
self.sum = Atomic(0)
self.var = Atomic([-1, 0])
示例3: Counter
class Counter(object):
"""
A counter is like a gauge, but you can increment or decrement its value ::
counter = Metrology.counter('pending-jobs')
counter.increment()
counter.decrement()
counter.count
"""
def __init__(self):
self._count = Atomic(0)
def increment(self, value=1):
"""Increment the counter. By default it will increment by 1.
:param value: value to increment the counter.
"""
self._count.update(lambda v: v + value)
def decrement(self, value=1):
"""Decrement the counter. By default it will decrement by 1.
:param value: value to decrement the counter.
"""
self._count.update(lambda v: v - value)
def clear(self):
self._count.value = 0
@property
def count(self):
"""Return the current value of the counter."""
return self._count.value
示例4: __init__
def __init__(self, reservoir_size, alpha):
self.values = RBTree()
self.counter = Atomic(0)
self.next_scale_time = Atomic(0)
self.alpha = alpha
self.reservoir_size = reservoir_size
self.lock = RLock()
self.clear()
示例5: __init__
def __init__(self, average_class=EWMA):
self.counter = Atomic(0)
self.start_time = time()
self.last_tick = Atomic(self.start_time)
self.interval = EWMA.INTERVAL
self.m1_rate = EWMA.m1()
self.m5_rate = EWMA.m5()
self.m15_rate = EWMA.m15()
示例6: EWMA
class EWMA(object):
INTERVAL = 5.0
SECONDS_PER_MINUTE = 60.0
ONE_MINUTE = 1
FIVE_MINUTES = 5
FIFTEEN_MINUTES = 15
M1_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / ONE_MINUTE)
M5_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIVE_MINUTES)
M15_ALPHA = 1 - math.exp(-INTERVAL / SECONDS_PER_MINUTE / FIFTEEN_MINUTES)
@classmethod
def m1(cls):
return EWMA(cls.M1_ALPHA, cls.INTERVAL)
@classmethod
def m5(cls):
return EWMA(cls.M5_ALPHA, cls.INTERVAL)
@classmethod
def m15(cls):
return EWMA(cls.M15_ALPHA, cls.INTERVAL)
def __init__(self, alpha, interval):
self.alpha = alpha
self.interval = interval
self.initialized = False
self._rate = 0.0
self._uncounted = Atomic(0)
def clear(self):
self.initialized = False
self._rate = 0.0
self._uncounted.value = 0
def update(self, value):
self._uncounted.update(lambda v: v + value)
def tick(self):
count = self._uncounted.swap(0)
instant_rate = count / self.interval
if self.initialized:
self._rate += self.alpha * (instant_rate - self._rate)
else:
self._rate = instant_rate
self.initialized = True
@property
def rate(self):
return self._rate
示例7: set_args
def set_args(self, args):
Atomic.set_args(self, args)
if 'graph' in self.args and self.args.graph:
self.graphdir = self.args.graph
else:
if os.path.exists("/var/lib/docker") and os.path.exists("/var/lib/docker-latest"):
raise ValueError("You must specify the --graph storage path to reset /var/lib/docker or /var/lib/docker-latest")
if os.path.exists("/var/lib/docker"):
self.graphdir = "/var/lib/docker"
else:
if os.path.exists("/var/lib/docker-latest"):
self.graphdir = "/var/lib/docker-latest"
else:
raise ValueError("Could not find any default graph storage path. Specify one using --graph option")
示例8: __init__
def __init__(self, alpha, interval):
self.alpha = alpha
self.interval = interval
self.initialized = False
self._rate = 0.0
self._uncounted = Atomic(0)
示例9: UniformSample
class UniformSample(object):
def __init__(self, reservoir_size):
self.counter = Atomic(0)
self.values = [0] * reservoir_size
def clear(self):
self.values = [0] * len(self.values)
self.counter.value = 0
def size(self):
count = self.counter.value
if count > len(self.values):
return len(self.values)
return count
def __len__(self):
return self.size
def snapshot(self):
return Snapshot(self.values[0:self.size()])
def update(self, value):
new_count = self.counter.update(lambda v: v + 1)
if new_count <= len(self.values):
self.values[new_count - 1] = value
else:
index = random.uniform(0, new_count)
if index < len(self.values):
self.values[int(index)] = value
示例10: UniqueIdSupplier
class UniqueIdSupplier(Supplier):
def __init__(self,
scheme):
super(UniqueIdSupplier, self).__init__()
self._scheme = scheme
self._id_count = Atomic(0)
def get(self):
new_id = self._id_count.update(lambda v: v + 1)
return UniqueId.of(self._scheme, str(new_id))
def get_with_value_prefix(self, value_prefix):
new_id = self._id_count.update(lambda v: v + 1)
return UniqueId.of(self._scheme, value_prefix + str(new_id))
def __str__(self):
return 'UniqueIdSupplier[' + self._scheme + ']'
示例11: __init__
def __init__(self, reservoir_size, alpha):
self.values = []
self.next_scale_time = Atomic(0)
self.alpha = alpha
self.reservoir_size = reservoir_size
self.lock = RLock()
self.rescale_threshold = ExponentiallyDecayingSample.calculate_rescale_threshold(alpha)
self.clear()
示例12: __init__
def __init__(self, average_class=EWMA):
self.counter = Atomic(0)
self.start_time = time()
self.m1_rate = EWMA.m1()
self.m5_rate = EWMA.m5()
self.m15_rate = EWMA.m15()
self.task = PeriodicTask(interval=average_class.INTERVAL,
target=self.tick)
self.task.start()
示例13: ObjectIdSupplier
class ObjectIdSupplier(Supplier):
"""
A supplier of object identifiers.
An object identifier consists of a scheme and value.
This class creates object identifiers for a fixed scheme name, where each
value is an incrementing number. The values are created in a thread-safe way.
This class is thread-safe and not externally mutable.
"""
def __init__(self,
scheme):
self._scheme = scheme
self._id_count = Atomic(0)
def get(self):
new_id = self._id_count.update(lambda v: v + 1)
return ObjectId.of(self._scheme, str(new_id))
def get_with_value_prefix(self, value_prefix):
new_id = self._id_count.update(lambda v: v + 1)
return ObjectId.of(self._scheme, value_prefix + str(new_id))
def __str__(self):
return 'ObjectIdSupplier[' + self._scheme + ']'
示例14: Derive
class Derive(Meter):
"""
A derive is like a meter but accepts an absolute counter as input.
derive = Metrology.derive('network.io')
derive.mark()
derive.count
"""
def __init__(self, average_class=EWMA):
self.last = Atomic(0)
super(Derive, self).__init__(average_class)
def mark(self, value=1):
"""Record an event with the derive.
:param value: counter value to record
"""
last = self.last.get_and_set(value)
if last <= value:
value = value - last
super(Derive, self).mark(value)
示例15: ExponentiallyDecayingSample
class ExponentiallyDecayingSample(object):
def __init__(self, reservoir_size, alpha):
self.values = []
self.next_scale_time = Atomic(0)
self.alpha = alpha
self.reservoir_size = reservoir_size
self.lock = RLock()
self.rescale_threshold = ExponentiallyDecayingSample.calculate_rescale_threshold(alpha)
self.clear()
@staticmethod
def calculate_rescale_threshold(alpha):
# determine rescale-threshold such that we will not overflow exp() in
# weight function, and subsequently not overflow into inf on dividing
# by random.random()
min_rand = 1.0 / (2**32) # minimum non-zero value from random()
safety = 2.0 # safety pad for numerical inaccuracy
max_value = sys.float_info.max * min_rand / safety
return math.log(max_value) / alpha
def clear(self):
with self.lock:
self.values = []
self.start_time = time()
self.next_scale_time.value = self.start_time + self.rescale_threshold
def size(self):
with self.lock:
return len(self.values)
def __len__(self):
return self.size()
def snapshot(self):
with self.lock:
return Snapshot(val for _, val in self.values)
def weight(self, timestamp):
return math.exp(self.alpha * (timestamp - self.start_time))
def rescale(self, now, next_time):
if self.next_scale_time.compare_and_swap(next_time, now + self.rescale_threshold):
with self.lock:
rescaleFactor = math.exp(-self.alpha * (now - self.start_time))
self.values = [(k * rescaleFactor, v) for k, v in self.values]
self.start_time = now
def rescale_if_necessary(self):
now = time()
next_time = self.next_scale_time.get_value()
if now > next_time:
self.rescale(now, next_time)
def update(self, value, timestamp=None):
if timestamp is None:
timestamp = time()
self.rescale_if_necessary()
with self.lock:
try:
priority = self.weight(timestamp) / random.random()
except (OverflowError, ZeroDivisionError):
priority = sys.float_info.max
if len(self.values) < self.reservoir_size:
heapq.heappush(self.values, (priority, value))
else:
heapq.heappushpop(self.values, (priority, value))