task.cancel
取消任务
task.cancel()
示例:
from usr import uasyncio as asyncio
async def bar(n):
print('Start cancellable bar()')
while True:
await asyncio.sleep(1)
n += 1
return n
async def do_cancel(task):
await asyncio.sleep(5)
print('About to cancel bar')
# 取消任务
task.cancel()
def main():
task = asyncio.wait_for(bar(10), 7)
asyncio.create_task(do_cancel(task))
asyncio.sleep(1)
asyncio.run(main())