Python3 中使用 asynckivy 启动的任务有多接近终止
How close kill a task started with asynckivy in Python3
在下面的代码中,一旦async scraper(self)
函数启动,它将一直异步运行,直到满足中断条件。
我怎样才能 kill/abort 过早地使用 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()
这不在文档中,但在 site_packages 中找到了。解决方案是:
# assign a variable to the Task
my_task = ak.start(scraper(self))
然后通过调用中止它:
my_task.cancel()
在下面的代码中,一旦async scraper(self)
函数启动,它将一直异步运行,直到满足中断条件。
我怎样才能 kill/abort 过早地使用 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()
这不在文档中,但在 site_packages 中找到了。解决方案是:
# assign a variable to the Task
my_task = ak.start(scraper(self))
然后通过调用中止它:
my_task.cancel()