python里创建任务执行一半时取消任务执行
2023-09-14 09:10:35 时间
下例子来演示创建任务执行一半时取消任务执行,这时会抛出异常CancelledError,同时也提供了一个机会来删除占用资源等等:
import asyncio
async def task_func():
print('in task_func, sleeping')
try:
await asyncio.sleep(1)
except asyncio.CancelledError:
print('task_func was canceled')
raise
return 'the result'
def task_canceller(t):
print('in task_canceller')
t.cancel()
print('canceled the task')
async def main(loop):
print('creating task')
task = loop.create_task(task_func())
loop.call_soon(task_canceller, task)
try:
await task
except asyncio.CancelledError:
print('main() also sees task as canceled')
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:
creating task
in task_func, sleeping
in task_canceller
canceled the task
task_func was canceled
main() also sees task as canceled
Python游戏开发入门
你也能动手修改C编译器
纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
相关文章
- Python进阶25-Django Web框架
- pycharm断点运行_python断点调试技巧
- Python自学教程1-安装pycharm和执行环境
- 简述python变量的命名规则_Python 变量命名规则
- 10个常用的损失函数解释以及Python代码实现
- 执行python程序的两种方式
- 怎么用python打开csv文件_Python文本处理之csv-csv文件怎么打开[通俗易懂]
- python setattr函数_Python内置函数(53)——setattr
- 【说站】python链表的乘法问题
- 【说站】python copy模块中的函数使用
- python调用通达信公式_通达信公式-主力雷达Python化[通俗易懂]
- Python 自动获取 Bing 壁纸
- python重复执行10次_卡bug
- lambda python表达式_Python的条件表达式和lambda表达式实例
- 用python编写猴子吃桃问题_人工智能猴子摘香蕉
- python定义函数求和_Python定义函数实现累计求和操作
- python自动化测试—Python自动化框架及工具
- python-高级协程编程-协程间的通信和数据传输(一)
- python-Python与MySQL数据库-使用Python执行MySQL查询
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与SQLite数据库-使用Python执行SQLite查询(二)
- Python编写的客户端给服务器发送指令执行相应的命令并返回结果详解编程语言
- python进阶详解元类及其应用1编程语言
- Linux创建Python文件的步骤(linux新建python文件)
- 掌握Linux环境下的Python编程(linux执行python)
- Python轻松连接SQL Server数据库(python连接sqlserver)
- 如何在Linux上将Python脚本设置为后台运行?(linux后台运行python)
- python复制文件代码实现
- python抓取京东价格分析京东商品价格走势
- 采用Psyco实现python执行速度提高到与编译语言一样的水平