Run_every (run_*) kwargs mystery

I’m going crazy here I’m obviously missing something, please help me out …

I’m trying to rewrite some automation scripts using callbacks to finally address thread starvation problems

VAR1 = "ABC"

  def initialize(self): 
    self.log("Starting CallbackTesting ...")
    self.handle1 = self.run_in(self.CallbackTesting1, test_interval, var1=VAR1)
    self.handle2 = self.run_in(self.CallbackTesting2, test_interval+1, var2=VAR1)
    self.handle3 = self.run_in(self.CallbackTesting3, test_interval+2)
    self.handle4 = self.run_every(self.CallbackTesting4, "now", 5, initial_check=True)

  def CallbackTesting1(self, kwargs):
    self.log("CallbackTesting1 : " + kwargs["var1"])

  def CallbackTesting2(self, kwargs):
    self.log("CallbackTesting2 : " + kwargs['var2'])

  def CallbackTesting3(self, kwargs):
    self.log("CallbackTesting3 : " + VAR1)

  def CallbackTesting4(self, kwargs):
    self.log("CallbackTesting4 : " + str(kwargs["initial_check"]))
    initial_check = kwargs["initial_check"]
    if initial_check:
      self.cancel_timer(self.handle4)
      initial_check = False
    self.log("CallbackTesting4 : Setting new run_every schedule")
    self.handle4 = self.run_every(self.CallbackTesting4, "now", 7)

  def terminate(self):
    self.cancel_timer(self.handle1)
    self.cancel_timer(self.handle2)
    self.cancel_timer(self.handle3)
    self.cancel_timer(self.handle4)

The problem lies within def CallbackTesting4(self, kwargs) (and any other def if additional code is added) where if I only have

  self.log("CallbackTesting4 : " + str(kwargs["initial_check"]))

it is working normally, the second I add the rest code lines I get
self.log("CallbackTesting4 : " + str(kwargs[“initial_check”]))
~~~~~~^^^^^^^^^^^^^^^^^
KeyError: ‘initial_check’

What am I missing?

PS.
I actually get this error no matter what kind of variable I try to pass bool, string, … (I the code above I’ve tried bool)

Thank you.

Don’t understand why

kwargs["initial_check"]

does not work but

self.log("CallbackTesting4 : " + str(kwargs.get('initial_check')))

(single quotes) seems to do the trick …

In CallbackTesting4, you’re recursively creating an additional scheduled callback with run_every() that doesn’t include the initial_check parameter. Not sure why you’re doing that though.

Thank you.

This was just my intro to understand how run_* are actually working as I needed to step away from time.sleep function which caused stale threads as I need my script to run 24/7 for which in AD recursive is a must.

I ended up with using kwargs.get(‘some_var_name’) rather than str(kwargs.get(‘some_var_name’))

I also went with run_in instead of run_every.