'Kivy: Running a Clock Scheduled Function for a Specified Amount of Time

Trying a write a Python/Kivy program to log data on a RasPi. I have a function that runs based off the clock schedule, started and stopped by user-input. This works fine because this code needs to run infinitely as it logs data from the GPIO.

    def start(self):
        Clock.unschedule(self.running)
        Clock.schedule_interval(self.running, .1)
        
    def running(self, interval):
        
        # single_setpoint() runs GPIO code
        MainApp.single_setpoint()
        
    def stop(self):
        Clock.unschedule(self.running)

The issue I'm running into is I now want the function which logs the data to do different things for different amounts of time. For example:

    def start(self):
        Clock.unschedule(self.running)
        Clock.schedule_interval(self.running, .1)
        
    def running(self, interval):
        
        # runs GPIO code for 10 seconds
        MainApp.10sec_setpoint()

        # runs different GPIO code for 5 seconds
        MainApp.5sec_setpoint()

        # runs more different GPIO code for 2 seconds
        MainApp.2sec_setpoint()
        
    def stop(self):
        Clock.unschedule(self.running)

I'm at a loss for how to actually do the timing for each chunk of new code. I know I can't "sleep" for that period of time since it will block UI stuff. Since running() is a new instance every time the clock scheduler calls it, I can't persist a value to check between time.time() and time.time() + 10.

Based on looking around I think I need to create a second thread that kicks off a timer when running() kicks off and then check against that elapsed time, but I can't figure out how to share the elapsed time value between the two?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source