Home > front end >  Kivy and infinite thread loop - app freezes
Kivy and infinite thread loop - app freezes

Time:12-12

I have to divide long tasks into threads. Creating thread anywhere in Kivy app makes the whole app wait for end of the thread function, so there is no difference when using threads or not. What am I doing wrong?

kv file:

BoxLayout:
    Button:
        on_press: threading.Thread(target=app.test()).start()
    Button:
        on_press: app.press()

python code:

class MyApp(App):
    running = True

    def on_stop(self):
        self.running = False

    def test(self):
        while self.running:
            print('test')
            time.sleep(2)

    def press(self):
        print('press')

if __name__ == '__main__':
    MyApp().run()

Once button is clicked and thread created, app freezes. How to create background thread?

CodePudding user response:

Thread(target=app.test()) calls app.test() immediately, which then enters the infinite loop. The target is the function itself, not its return value. The code should look like this (without parentheses):

threading.Thread(target=app.test).start()
  • Related