我是靠谱客的博主 留胡子夕阳,这篇文章主要介绍python取消任务的方法_python里取消一个未曾执行的任务,现在分享给大家,希望可以做个参考。

python里取消一个未曾执行的任务

下面的例子使用create_task()函数来创建一个任务,在未执行任务之前就进行取消:

import asyncio

async def task_func():

print('in task_func')

return 'the result'

async def main(loop):

print('创建任务')

task = loop.create_task(task_func())

print('取消任务')

task.cancel()

print('canceled task {!r}'.format(task))

try:

await task

except asyncio.CancelledError:

print('caught error from canceled task')

else:

print('task result: {!r}'.format(task.result()))

event_loop = asyncio.get_event_loop()

try:

event_loop.run_until_complete(main(event_loop))

finally:

event_loop.close()

结果输出如下:

创建任务

取消任务

canceled task >

caught error from canceled task

Python游戏开发入门

你也能动手修改C编译器

纸牌游戏开发

五子棋游戏开发

最后

以上就是留胡子夕阳最近收集整理的关于python取消任务的方法_python里取消一个未曾执行的任务的全部内容,更多相关python取消任务内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部