本文整理汇总了Python中mpx.lib.threading.Lock.locked方法的典型用法代码示例。如果您正苦于以下问题:Python Lock.locked方法的具体用法?Python Lock.locked怎么用?Python Lock.locked使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mpx.lib.threading.Lock
的用法示例。
在下文中一共展示了Lock.locked方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from mpx.lib.threading import Lock [as 别名]
# 或者: from mpx.lib.threading.Lock import locked [as 别名]
class _Lock:
def __init__(self):
self._minutes = 0
self._lock = Lock()
self._scheduled = None
self._stack = None
def acquire(self,blocking=0):
value = self._lock.acquire(blocking)
self._stack = traceback.extract_stack()
self._schedule_print()
return value
def release(self):
try:
if self._scheduled:
self._scheduled.cancel()
finally:
self._lock.release()
def locked(self):
return self._lock.locked()
def _schedule_print(self):
self._scheduled = scheduler.after(60,self._print,(self._stack,))
def _print(self,stack):
self._minutes += 1
print 'Lock acquired: %s min' % self._minutes
print string.join(traceback.format_list(stack))
if self.locked():
self._schedule_print()
示例2: EnergywiseDomain
# 需要导入模块: from mpx.lib.threading import Lock [as 别名]
# 或者: from mpx.lib.threading.Lock import locked [as 别名]
#.........这里部分代码省略.........
result = switch.cpex_domain_usage(importance)
if result:
aggr_result += result
break
except:
pass
except ENoDomainSwitches:
raise
finally:
self._cpex_lock.release()
return aggr_result
# caching for cpex switches under a domain
# @return A dictionary of Energywise usage values keyed by switch address, possibly from cache.
def cpex_switch_usage_map(self, importance=100, skipCache=False):
self._cpex_switch_map_lock.acquire()
try:
if skipCache or self._is_cpex_switch_map_stale(time.time()):
#fetch actual value
usage_map = self._cpex_switch_usage_map(importance)
self._update_cpex_switch_map(usage_map, time.time())
return self._cpex_switch_map_cache
finally:
self._cpex_switch_map_lock.release()
raise EUnreachableCode("Executed unreachable code!")
##
# Call cpex_switch_usage_map on the primary cpex switch.
# @return A dictionary of Energywise usage values keyed by switch address.
def _cpex_switch_usage_map(self, importance=100, max_attempts=3):
self._cpex_lock.acquire()
aggr_result = {}
try:
if not self._cpex_switches:
raise ENoDomainSwitches(
"No switches are configured for the %r domain." % self.domain
)
for switch in self._cpex_switches:
for i in range(0, max_attempts):
try:
result = switch.cpex_switch_usage_map(importance)
if result:
aggr_result.update(result)
break
except:
pass
except ENoDomainSwitches:
raise
finally:
self._cpex_lock.release()
return aggr_result
def snmp_domain_usage(self, importance=100, skipCache=False):
result = 0
for switch in self._snmp_switches:
try:
result += switch.snmp_switch_usage(importance, skipCache)
except:
msglog.exception()
msglog.log("Energywise",msglog.types.ERR,
"Failed to get data from %r switch" %switch.name
)
return result
def _is_cpex_switch_map_stale(self, timestamp):
return (self._cpex_switch_map_time + self.ttl) < timestamp
def _update_cpex_switch_map(self, switch_map, timestamp):
self._cpex_switch_map_cache = SwitchMap(switch_map)
self._cpex_switch_map_time = timestamp
def _is_domain_cache_stale(self,timestamp):
assert self._cache_lock.locked()
return (self._cache_time + self.ttl) < timestamp
def _update_domain_cache(self,value, timestamp):
self._cache_value = value
self._cache_time = timestamp
#caching the domain-usage
def aggregate_domain_usage(self, importance=100, skipCache=False):
self._cache_lock.acquire()
try:
if skipCache or self._is_domain_cache_stale(time.time()):
#fetch the actual value and update the cache
try:
result = self.energywise_domain_usage(importance, skipCache)
except:
msglog.exception()
result = 0
for sub in self._all_domains:
result += sub.aggregate_domain_usage(importance, skipCache)
self._update_domain_cache(result, time.time())
return self._cache_value
else:
# use the cached value
return self._cache_value
finally:
self._cache_lock.release()
raise EUnreachableCode("Executed unreachable code!")
def new_trend(self,period):
return new_trend(self,period)
def delete_trend(self):
return delete_trend(self)
def get(self, skipCache=False):
return self.aggregate_domain_usage()
def get_result(self, skipCache=False):
return Result(self.get(skipCache), time.time(), cached=False)