(点击
上方蓝字
,快速关注我们)
来源:人
世间
www.jianshu.com/p/b5e347b3a17c
如有好文章投稿,请点击 → 这里了解详情
python asyncio
网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程。无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态。使用协程可以实现高效的并发任务。Python的在3.4中引入了协程的概念,可是这个还是以生成器对象为基础,3.5则确定了协程的语法。下面将简单介绍asyncio的使用。实现协程的不仅仅是asyncio,tornado和gevent都实现了类似的功能。
-
event_loop 事件循环:程序开启一个无限的循环,程序员会把一些函数注册到事件循环上。当满足事件发生的时候,调用相应的协程函数。
-
coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。
-
task 任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含任务的各种状态。
-
future: 代表将来执行或没有执行的任务的结果。它和task上没有本质的区别
-
async/await 关键字:python3.5 用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。
上述的概念单独拎出来都不好懂,比较他们之间是相互联系,一起工作。下面看例子,再回溯上述概念,更利于理解。
定义一个协程
定义一个协程很简单,使用async关键字,就像定义普通函数一样:
import time
import asyncio
now
=
lambda
:
time
.
time
()
async def do_some_work
(
x
)
:
print
(
'Waiting: '
,
x
)
start
=
now
()
coroutine
=
do_some_work
(
2
)
loop
=
asyncio
.
get_event_loop
()
loop
.
run_until_complete
(
coroutine
)
print
(
'TIME: '
,
now
()
-
start
)
通过async关键字定义一个协程(coroutine),协程也是一种对象。协程不能直接运行,需要把协程加入到事件循环(loop),由后者在适当的时候调用协程。asyncio.get_event_loop方法可以创建一个事件循环,然后使用run_until_complete将协程注册到事件循环,并启动事件循环。因为本例只有一个协程,于是可以看见如下输出:
Waiting
:
2
TIME
:
0.0004658699035644531
创建一个task
协程对象不能直接运行,在注册事件循环的时候,其实是run_until_complete方法将协程包装成为了一个任务(task)对象。所谓task对象是Future类的子类。保存了协程运行后的状态,用于未来获取协程的结果。
import asyncio
import time
now
=
lambda
:
time
.
time
()
async def do_some_work
(
x
)
:
print
(
'Waiting: '
,
x
)
start
=
now
()
coroutine
=
do_some_work
(
2
)
loop
=
asyncio
.
get_event_loop
()
# task = asyncio.ensure_future(coroutine)
task
=
loop
.
create_task
(
coroutine
)
print
(
task
)
loop
.
run_until_complete
(
task
)
print
(
task
)
print
(
'TIME: '
,
now
()
-
start
)
可以看到输出结果为:
Task pending
coro
=
do_some_work
()
running
at
/
Users
/
ghost
/
Rsj217
/
python3
.
6
/
async
/
async
-
main
.
py
:
17
>>
Waiting
:
2
Task finished
coro
=
do_some_work
()
done
,
defined
at
/
Users
/
ghost
/
Rsj217
/
python3
.
6
/
async
/
async
-
main
.
py
:
17
>
result
=
None
>
TIME
:
0.0003490447998046875
创建task后,task在加入事件循环之前是pending状态,因为do_some_work中没有耗时的阻塞操作,task很快就执行完毕了。后面打印的finished状态。
asyncio.ensure_future(coroutine) 和 loop.create_task(coroutine)都可以创建一个task,run_until_complete的参数是一个futrue对象。当传入一个协程,其内部会自动封装成task,task是Future的子类。isinstance(task, asyncio.Future)将会输出True。
绑定回调
绑定回调,在task执行完毕的时候可以获取执行的结果,回调的最后一个参数是future对象,通过该对象可以获取协程返回值。如果回调需要多个参数,可以通过偏函数导入。
import time
import asyncio
now
=
lambda
:
time
.
time
()
async def do_some_work
(
x
)
:
print
(
'Waiting: '
,
x
)
return
'Done after {}s'
.
format
(
x
)
def callback
(
future
)
:
print
(
'Callback: '
,
future
.
result
())
start
=
now
()
coroutine
=
do_some_work
(
2
)
loop
=
asyncio
.
get_event_loop
()
task
=
asyncio
.
ensure_future
(
coroutine
)
task
.
add_done_callback
(
callback
)
loop
.
run_until_complete
(
task
)
print
(
'TIME: '
,
now
()
-
start
)
def callback
(
t
,
future
)
:
print
(
'Callback:'
,
t
,
future
.
result
())
task
.
add_done_callback
(
functools
.
partial
(
callback
,
2
))
可以看到,coroutine执行结束时候会调用回调函数。并通过参数future获取协程执行的结果。我们创建的task和回调里的future对象,实际上是同一个对象。
future 与 result
回调一直是很多异步编程的恶梦,程序员更喜欢使用同步的编写方式写异步代码,以避免回调的恶梦。回调中我们使用了future对象的result方法。前面不绑定回调的例子中,我们可以看到task有fiinished状态。在那个时候,可以直接读取task的result方法。
async def do_some_work
(
x
)
:
print
(
'Waiting {}'
.
format
(
x
))
return
'Done after {}s'
.
format
(
x
)
start
=
now
()
coroutine
=
do_some_work
(
2
)
loop
=
asyncio
.
get_event_loop
()
task
=
asyncio
.
ensure_future
(
coroutine
)
loop
.
run_until_complete
(
task
)
print
(
'Task ret: {}'
.
format
(
task
.
result
()))
print
(
'TIME: {}'
.
format
(
now
()
-
start
))
可以看到输出的结果:
Waiting
:
2
Task
ret
:
Done
after
2s
TIME
:
0.0003650188446044922
阻塞和await
使用async可以定义协程对象,使用await可以针对耗时的操作进行挂起,就像生成器里的yield一样,函数让出控制权。协程遇到await,事件循环将会挂起该协程,执行别的协程,直到其他的协程也挂起或者执行完毕,再进行下一个协程的执行。
耗时的操作一般是一些IO操作,例如网络请求,文件读取等。我们使用asyncio.sleep函数来模拟IO操作。协程的目的也是让这些IO操作异步化。
import asyncio
import time
now
=
lambda
:
time
.
time
()
async def do_some_work
(
x
)
:
print
(
'Waiting: '
,
x
)
await
asyncio
.
sleep
(
x
)
return
'Done after {}s'
.
format
(
x
)
start
=
now
()
coroutine
=
do_some_work
(
2
)
loop
=
asyncio
.
get_event_loop
()
task
=
asyncio
.
ensure_future
(
coroutine
)
loop
.
run_until_complete
(
task
)
print
(
'Task ret: '
,
task
.
result
())
print
(
'TIME: '
,
now
()
-
start
)
在 sleep的时候,使用await让出控制权。即当遇到阻塞调用的函数的时候,使用await方法将协程的控制权让出,以便loop调用其他的协程。现在我们的例子就用耗时的阻塞操作了。
并发和并行
并发和并行一直是容易混淆的概念。并发通常指有多个任务需要同时进行,并行则是同一时刻有多个任务执行。用上课来举例就是,并发情况下是一个老师在同一时间段辅助不同的人功课。并行则是好几个老师分别同时辅助多个学生功课。简而言之就是一个人同时吃三个馒头还是三个人同时分别吃一个的情况,吃一个馒头算一个任务。
asyncio实现并发,就需要多个协程来完成任务,每当有任务阻塞的时候就await,然后其他协程继续工作。创建多个协程的列表,然后将这些协程注册到事件循环中。
import asyncio
import time
now
=
lambda
:
time
.
time
()
async def do_some_work
(
x
)
:
print
(
'Waiting: '
,
x
)
await
asyncio
.
sleep
(
x
)
return
'Done after {}s'
.
format
(
x
)
start
=
now
()
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: '
,
now
()
-
start
)
结果如下
Waiting
:
1
Waiting
:
2
Waiting
:
4
Task
ret
:
Done
after
1s
Task
ret
:
Done
after
2s
Task
ret
:
Done
after
4s
TIME
:
4.003541946411133
总时间为4s左右。4s的阻塞时间,足够前面两个协程执行完毕。如果是同步顺序的任务,那么至少需要7s。此时我们使用了aysncio实现了并发。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一个task列表,后者接收一堆task。
协程嵌套
使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程,这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来。
import asyncio
import time
now
=
lambda
:
time
.
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
)
]
dones
,
pendings
=
await
asyncio
.
wait
(
tasks
)
for
task
in
dones
:
print
(
'Task ret: '
,
task
.
result
())
start
=
now
()
loop
=
asyncio
.
get_event_loop
()
loop
.
run_until_complete
(
main
())
print
(
'TIME: '
,
now
()
-
start
)
如果使用的是 asyncio.gather创建协程对象,那么await的返回值就是协程运行的结果。
results
=
await
asyncio
.
gather
(
*
tasks
)
for
result
in
results
:
print
(
'Task ret: '
,
result
)
不在main协程函数里处理结果,直接返回await的内容,那么最外层的run_until_complete将会返回main协程的结果。
async def main
()
:
coroutine1
=
do_some_work
(
1
)
coroutine2
=
do_some_work
(
2
)
coroutine3
=
do_some_work
(
2
)
tasks
=
[
asyncio
.
ensure_future
(
coroutine1
),
asyncio
.
ensure_future
(
coroutine2
),
asyncio
.
ensure_future
(
coroutine3