How to interrupt an executing function in kivy?

111 views Asked by At

I am developing an app in Kivy and have one function that seems to take a long time to finish. Therefore, when the button that calls this function is pressed, i first open a modalview with a progress bar. I now want to update the value in the progressbar every 500ms while the main function is executing

My first attempt was to use Clock.schedule_interval(). Pseudo code looks something like this:

Class MainClass(Screen):

    def button_callback_function(self):

        #open modalview with progress bar
        view = ModalView(autodismiss=True)
        view.open()

        Clock.schedule_interval(self.update_progress_bar_in_modalview,0.5) 

        self.function_that_takes_too_long()


    def function_that_takes_too_long(self):
       /* code here*/

    def update_progress_bar_in_modalview(self,dt):
       /*updates value of progress bar*/

With this code, the progress bar does indeed get updated but only after function_that_takes_too_long() finishes, not parallel to it.

My second attempt was to use python threads:

Class MainClass(Screen):

    def button_callback_function(self):

        #open modalview with progress bar
        view = ModalView(autodismiss=True)
        view.open()

        x=threading.Thread(target=self.update_progress_bar_in_modalview)
        x.start()

        self.function_that_takes_too_long()


    def function_that_takes_too_long(self):
       /* code here*/

    def update_progress_bar_in_modalview(self):
       /*updates value of progress bar*/
       timer.sleep(0.5)

Here the second thread to update the progress bar is never even started. It seems the main thread has to pause to give the second thread a chance to start.

So is there any way to call update_progress_bar_in_modalview() while function_that_takes_too_long() is still executing? Something like periodic interrupts in micro controllers. Or maybe start the thread that updates the progress bar on a separate core?

I appreciate any hints.

Alex

0

There are 0 answers