Search code examples
python-3.xkivykivy-languagekivymd

How close kill a task started with asynckivy in Python3


In the following code, once the async scraper(self) function is initiated, it will run asynchronously forever until the break condition is met.

How can I kill/abort this function prematurely using asynckivy?

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
import asynckivy as ak

class MyBotLayout(TabbedPanel):

    def pressStart(self):
        self.bot_start.disabled = True
        self.spider_status.text = "Running"
        
        # How to kill this async function below?
        async def scraper(self):
            while 1:
                # do something until break
                if break_condition_met:
                    break
        ak.start(scraper(self))



class MyBotApp(App):
    def build(self):
        return MyBotLayout()

MyBotApp().run()

Solution

  • This wasn't in docs, but found it in site_packages. The solution was to:

    # assign a variable to the Task
    my_task = ak.start(scraper(self))
    

    And then abort it by calling:

    my_task.cancel()