本文整理汇总了Python中timer.Timer.update_interval方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.update_interval方法的具体用法?Python Timer.update_interval怎么用?Python Timer.update_interval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timer.Timer
的用法示例。
在下文中一共展示了Timer.update_interval方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from timer import Timer [as 别名]
# 或者: from timer.Timer import update_interval [as 别名]
class Plugin:
def __init__(self, file, plugin_path, ttl):
self.file = file # linux/cpu.sh
self.name = file.rpartition('/')[2] # cpu.sh
self.plugin_path = plugin_path
self.running = False
self.logger = logging.getLogger('default')
self.datastore = datastore.datastore(ttl)
# Set default values
self.interval = 10
# Create thread with timer
self.t = Timer(self.interval, self.execute)
def start(self):
params = (self.file, self.interval)
self.logger.debug('Start polling %s with interval %s' % params)
self.running = True
self.execute()
self.t.start()
def stop(self):
self.logger.info('Stop polling ' + self.file)
self.running = False
self.t.stop()
def status(self):
return self.running
def execute(self, args=None):
self.logger.debug('Running ' + self.file)
try:
start = time.time()
proc = subprocess.Popen([self.plugin_path + '/' + self.file],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
successful = False
stdout, stderr = proc.communicate()
if proc.returncode is 0:
for line in stdout.split('\n'):
match = re.match(r'^([^ ]+)\.value (.+)$', line)
if line and match:
(key, value) = match.groups()
self.datastore.push(key, value)
successful = True
if not successful:
self.logger.warn('No valid output from ' + self.file)
else:
code = proc.returncode
self.logger.error('Plugin returned with exit code %s' % code)
elapsed = time.time() - start
# Warn if execution takes more time than the interval
if elapsed > self.interval:
params = (self.interval, round(elapsed), self.file)
message = 'Execution exceeds interval (%ss > %ss): %s' % params
self.logger.warn(message)
except OSError:
self.logger.error('Failed to execute plugin: ' + self.file)
def update_interval(self, new_interval):
self.t.update_interval(new_interval)