禁用 VOLTTRON 中的计划功能
Disable schedule function in VOLTTRON
要定期执行函数,使用以下命令。
self.core.schedule(periodic(t), periodic_function)
我想在满足某些条件时禁用上述功能。有人知道怎么做吗?
@GYOON
我会按照此处的代码执行操作:https://github.com/VOLTTRON/volttron/blob/master/services/core/VolttronCentralPlatform/vcplatform/agent.py#L320
基本上是在代理的 onstart/onconfig 方法中发生的,一个要执行的函数在稍后的 greenlet 中被调用
class MyAgent(Agent):
def __init__(self, **kwargs):
self._periodic_event = None
self.enabled = True
@Core.receiver('onstart')
def onstart(self, sender, **kwargs):
self.core.spawn_later(1, self._my_periodic_event)
def _my_periodic_event(self):
if self._periodic_event is not None:
self._periodic_event.cancel()
# do stuff here within this event loop
if self.enabled:
# note this is an internal volttron function see the referenced link for
# import
now = get_aware_utc_now()
next_update_time = now + datetime.timedelta(seconds=20)
self._periodic_event = self.core.schedule(next_update_time, self._my_periodic_event)
这样做的好处是您可以完全控制调度过程。启用、禁用、何时开始等。如果需要,可以使用成员变量更改秒数。
再次抱歉回复晚了,但希望这对您有所帮助!
要定期执行函数,使用以下命令。
self.core.schedule(periodic(t), periodic_function)
我想在满足某些条件时禁用上述功能。有人知道怎么做吗?
@GYOON
我会按照此处的代码执行操作:https://github.com/VOLTTRON/volttron/blob/master/services/core/VolttronCentralPlatform/vcplatform/agent.py#L320
基本上是在代理的 onstart/onconfig 方法中发生的,一个要执行的函数在稍后的 greenlet 中被调用
class MyAgent(Agent):
def __init__(self, **kwargs):
self._periodic_event = None
self.enabled = True
@Core.receiver('onstart')
def onstart(self, sender, **kwargs):
self.core.spawn_later(1, self._my_periodic_event)
def _my_periodic_event(self):
if self._periodic_event is not None:
self._periodic_event.cancel()
# do stuff here within this event loop
if self.enabled:
# note this is an internal volttron function see the referenced link for
# import
now = get_aware_utc_now()
next_update_time = now + datetime.timedelta(seconds=20)
self._periodic_event = self.core.schedule(next_update_time, self._my_periodic_event)
这样做的好处是您可以完全控制调度过程。启用、禁用、何时开始等。如果需要,可以使用成员变量更改秒数。
再次抱歉回复晚了,但希望这对您有所帮助!