定义一个协程并创建tasks:
import asyncio
import time
# 我们通过async关键字定义一个协程,当然协程不能直接运行,需要将协程加入到事件循环loop中
async def do_some_work(x):
print("waiting:", x)
start = time.time()
coroutine = do_some_work(2)
loop = asyncio.get_event_loop() # asyncio.get_event_loop:创建一个事件循环
# 通过loop.create_task(coroutine)创建task,同样的可以通过 asyncio.ensure_future(coroutine)创建task
task = loop.create_task(coroutine) # 创建任务, 不立即执行
loop.run_until_complete(task) # 使用run_until_complete将协程注册到事件循环,并启动事件循环
print("Time:",time.time() - start)
asyncio绑定回调:
import asyncio
import time
# 我们通过async关键字定义一个协程,当然协程不能直接运行,需要将协程加入到事件循环loop中
async def do_some_work(x):
print("waiting:", x)
return "Done after {}s".format(x)
def callback(future):
print("callback:",future.result())
start = time.time()
coroutine = do_some_work(2)
loop = asyncio.get_event_loop() # asyncio.get_event_loop:创建一个事件循环
# 通过loop.create_task(coroutine)创建task,同样的可以通过 asyncio.ensure_future(coroutine)创建task
task = loop.create_task(coroutine) # 创建任务, 不立即执行
# task = asyncio.ensure_future(coroutine)
task.add_done_callback(callback)
# 绑定回调,在task执行完成的时候可以获取执行的结果
loop.run_until_complete(task) # 使用run_until_complete将协程注册到事件循环,并启动事件循环
print("Time:",time.time() - start)
''' 运行结果
waiting: 2
callback: Done after 2s
Time: 0.0010030269622802734
普通串行花费7秒:
# 普通串行花费7秒
import time
def do_some_work(t):
time.sleep(t)
print('用了%s秒' % t)
start = time.time()
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(4)
print(time.time()-start)
'''
用了1秒
用了2秒
用了4秒
7.002151012420654
'''
使用协程并发执行只花费4秒:
# 使用协程并发执行只花费4秒
import asyncio
import time
async def do_some_work(x):
print("Waiting:",x)
await asyncio.sleep(x)
return "Done after {}s".format(x)
start = time.time()
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(4)
tasks = [
asyncio.ensure_future(coroutine1),
asyncio.ensure_future(coroutine2),
asyncio.ensure_future(coroutine3)
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
for task in tasks:
print("Task ret:",task.result())
print("Time:",time.time() - start)
'''
Waiting: 1
Waiting: 2
Waiting: 4
Task ret: Done after 1s
Task ret: Done after 2s
Task ret: Done after 4s
Time: 4.0038135051727295
'''
1)协程嵌套写法
协程嵌套 普通写法:
# 1. 使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程
# 2. 这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来。import asyncio
import time
import asyncio
async def do_some_work(x):
print("waiting:",x)
await asyncio.sleep(x)
return "Done after {}s".format(x)
async def main():
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(4)
tasks = [
asyncio.ensure_future(coroutine1),
asyncio.ensure_future(coroutine2),
asyncio.ensure_future(coroutine3)
]
dones, pendings = await asyncio.wait(tasks)
for task in dones:
print("Task ret:", task.result())
# results = await asyncio.gather(*tasks)
# for result in results:
# print("Task ret:",result)
start = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print("Time:", time.time() - start)
'''
waiting: 1
waiting: 2
waiting: 4
Task ret: Done after 1s
Task ret: Done after 2s
Task ret: Done after 4s
Time: 4.003407716751099
'''
协程嵌套 使用asyncio.wait方式挂起协程:
# 或者返回使用asyncio.wait方式挂起协程
import asyncio
import time
async def do_some_work(x):
print("waiting:",x)
await asyncio.sleep(x)
return "Done after {}s".format(x)
async def main():
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(4)
tasks = [
asyncio.ensure_future(coroutine1),
asyncio.ensure_future(coroutine2),
asyncio.ensure_future(coroutine3)
]
return await asyncio.wait(tasks)
start = time.time()
loop = asyncio.get_event_loop()
done,pending = loop.run_until_complete(main())
for task in done:
print("Task ret:",task.result())
print("Time:", time.time() - start)
'''
waiting: 1
waiting: 2
waiting: 4
Task ret: Done after 1s
Task ret: Done after 2s
Task ret: Done after 4s
Time: 4.002181529998779
'''
协程嵌套 使用列表推导式简写:
import time
import asyncio
async def job(t): # 使用 async 关键字将一个函数定义为协程
await asyncio.sleep(t) # 等待 t 秒, 期间切换执行其他任务
print('用了%s秒' % t)
async def main(loop): # 使用 async 关键字将一个函数定义为协程
tasks = [loop.create_task(job(t)) for t in range(1,3)] # 创建任务, 不立即执行
await asyncio.wait(tasks) # 执行并等待所有任务完成
start = time.time()
loop = asyncio.get_event_loop() # 创建一个事件loop
loop.run_until_complete(main(loop)) # 将事件加入到事件循环loop
loop.close() # 关闭 loop
print(time.time()-start)
'''
用了1秒
用了2秒
2.0013420581817627
'''