'Set and schedule a task to run.
Args:
seconds (int, float): the delay in seconds from now.
obj (Object): the typecalssed object connected to the event.
callback_name (str): the callback\'s name.
Notes:
This method allows to schedule a "persistent" task.
\'utils.delay\' is called, but a copy of the task is kept in
the event handler, and when the script restarts (after reload),
the differed delay is called again.
The dictionary of locals is frozen and will be available
again when the task runs.  This feature, however, is limited
by the database: all data cannot be saved.  Lambda functions,
class methods, objects inside an instance and so on will
not be kept in the locals dictionary.'

def set_task(self, seconds, obj, callback_name):

    now = datetime.now()
    delta = timedelta(seconds=seconds)
    used_ids = list(self.db.tasks.keys())
    task_id = 1
    while (task_id in used_ids):
        task_id += 1
    locals = {}
    for (key, value) in self.ndb.current_locals.items():
        try:
            dbserialize(value)
        except TypeError:
            continue
        else:
            locals[key] = value
    self.db.tasks[task_id] = ((now + delta), obj, callback_name, locals)
    delay(seconds, complete_task, task_id)
