0

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

Alex
  • 57
  • 2
  • 8
  • Did you try using clock within thread in your second approach (or maybe binding some kv_prop.) ? – ApuCoder Jul 01 '22 at 17:02
  • 1
    You should run the `function_that_takes_too_long()` in the thread instead of the `update_progress_bar_in_modalview()`. Then you can add the `@mainthread` decorator to the `update_progress_bar_in_modalview()`, call that method occaisionally from the `function_that_takes_too_long()`, ad remove the `sleep`. – John Anderson Jul 01 '22 at 20:20
  • @JohnAnderson: Exactly what i needed. Thank you for pointing our the thread decorators – Alex Jul 15 '22 at 10:15

0 Answers0