python里创建一个任务(Task)
2017-09-08 15:52
489 查看
与事件循环进行交互,最基本的方式就是任务,任务封装了协程和自动跟踪它的状态。任务是Future类的子类,所以其它协程可以等待任务完成,或当这些任务完成获取返回结果。
在这里通过create_task()函数来创建一个任务实例,然后事件循环就运行这个任务,直到这个任务返回为止:
import asyncio
async def task_func():
print('in task_func')
return 'the result'
async def main(loop):
print('creating task')
task = loop.create_task(task_func())
print('waiting for {!r}'.format(task))
return_value = await task
print('task completed {!r}'.format(task))
print('return value: {!r}'.format(return_value))
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:creating task
waiting for <Task pending coro=<task_func() running at D:\work\csdn\python_Game1\example\asyncio_create_task.py:4>>
in task_func
task completed <Task finished coro=<task_func() done, defined at D:\work\csdn\python_Game1\example\asyncio_create_task.py:4> result='the result'>
return value: 'the result'
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
在这里通过create_task()函数来创建一个任务实例,然后事件循环就运行这个任务,直到这个任务返回为止:
import asyncio
async def task_func():
print('in task_func')
return 'the result'
async def main(loop):
print('creating task')
task = loop.create_task(task_func())
print('waiting for {!r}'.format(task))
return_value = await task
print('task completed {!r}'.format(task))
print('return value: {!r}'.format(return_value))
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:creating task
waiting for <Task pending coro=<task_func() running at D:\work\csdn\python_Game1\example\asyncio_create_task.py:4>>
in task_func
task completed <Task finished coro=<task_func() done, defined at D:\work\csdn\python_Game1\example\asyncio_create_task.py:4> result='the result'>
return value: 'the result'
Python游戏开发入门
http://edu.csdn.net/course/detail/5690你也能动手修改C编译器
http://edu.csdn.net/course/detail/5582纸牌游戏开发
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
相关文章推荐
- 在ArcGIS Server Java ADF中创建一个自定义task的简单步骤
- Asp.Net MVC(创建一个任务列表应用程序) - Part.1
- Python----一个对象的属性可以是另外一个类型创建的对象
- Android程序 类QQ通知常驻在Notification栏 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例
- python用法: post一个http请求, schedule一个task
- 使用Create task with ContentType创建任务的时候,必须先在task list中加上该ContentType
- Android程序 类QQ通知常驻在Notification栏 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例
- python 在windows下创建文件IOError的一个原因
- Virtual Box 报错,无法为虚拟电脑xxxx创建一个新任务
- 如何用Python创建一个多层神经网络
- python使用wxPython创建一个简单的文本编辑器。
- Anaconda还可以在python3.5的基础上创建一个新的独立的python2.7的环境
- IronPython for ASP.NET:使用IronPython创建一个简单的Web页面
- Python3 Tkinter基础 Entry validate='key ' isdigit 创建一个只能输入数字的输入框
- Python:在Linux下创建一个脚本并且自动修改权限,可以选择路径。批量生成脚本 。
- 一个很有意思的东西,Python EGG(蟒蛇蛋) 创建Python Egg创建Python Egg
- cron job的方式定时备份Redis的数据文件,并将备份文件copy到安全的磁盘介质中。创建一个定期任务(cron job)
- hdu 3433 A Task Process N个人,第i个人完成一个A任务需要时间ai,完成一个B任务需要时间bi, 现在又X个任务A和Y个任务B,求完成所有任务所需要的最短时间。
- 创建一个可执行的python脚本
- Python 3基础教程38-threading和创建一个线程