Search code examples
python-decoratorsvolttron

Core.schedule decorator in VOLTTRON


How does one use the Core.schedule() decorator to schedule a task to start at a specific time within an agent? I tried using Core.schedule(deadline, *args, **kwargs) where deadline is the date and time (i.e. '2016-09-26 10:00:00'), but received the error args is not defined.


Solution

  • Typically the schedule method is not used as a decorator as the scheduled callback is only called once. (I've never actually done it.)

    The *args and **kwargs are place holders for variable arguments that get passed to a function. See http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

    In this case the schedule method uses *args and **kwargs to store any extra arguments passed into the decorator which are then passed into the called function later.

    For instance if I have an agent method update_state(self, parameter1) and I want to schedule that to run at some time in the future specified by a datetime object called update_time and the value 42 for the value of parameter1 I could do:

    self.core.schedule(update_time, self.update_state, 42)
    

    update_time needs to be a python datetime object or a unix time stamp.

    To use it as a decorator you would do something like this:

    Core.schedule(update_time, 42)
    def update_state(self, parameter1):
        pass
    

    When update_time arrives it will be called once. If you want to schedule another call to the update_state function you would need to figure out the new time and schedule it from the update_state function.