本文整理汇总了Python中atomic.Atomic.swap方法的典型用法代码示例。如果您正苦于以下问题:Python Atomic.swap方法的具体用法?Python Atomic.swap怎么用?Python Atomic.swap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类atomic.Atomic
的用法示例。
在下文中一共展示了Atomic.swap方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EWMA
# 需要导入模块: from atomic import Atomic [as 别名]
# 或者: from atomic.Atomic import swap [as 别名]
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):
with self._uncounted:
self._uncounted.value += 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
示例2: test_swap
# 需要导入模块: from atomic import Atomic [as 别名]
# 或者: from atomic.Atomic import swap [as 别名]
def test_swap(self):
atomic = Atomic(1000)
swapped = atomic.swap(1001)
self.assertEqual(1001, atomic.value)
self.assertEqual(1000, swapped)