我们都知道,现在的服务器开发对于IO调度的优先级控制权已经不再依靠系统,都希望采用协程的方式实现高效的并发任务,如js、lua等在异步协程方面都做的很强大。
Python在3.4版本也加入了协程的概念,并在3.5确定了基本完善的语法和实现方式。同时3.6也对其进行了如解除了await和yield在同一个函数体限制等相关的优化。
event_loop 事件循环:程序开启一个无限的循环,程序员会把一些函数注册到事件循环上。当满足事件发生的时候,调用相应的协程函数。
coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。
task 任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含任务的各种状态。
future: 代表将来执行或没有执行的任务的结果。它和task上没有本质的区别
async/await 关键字:python3.5 用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。
【一】创建协程
首先定义一个协程,在def前加入async声明,就可以定义一个协程函数。
一个协程函数不能直接调用运行,只能把协程加入到事件循环loop中。asyncio.get_event_loop方法可以创建一个事件循环,然后使用run_until_complete将协程注册到事件循环,并启动事件循环。
例如:
- import asyncio
- async def fun():
- print('hello word')
- loop = asyncio.get_event_loop()
- loop.run_until_complete(fun())
【二】任务对象task
协程对象不能直接运行,在注册事件循环的时候,其实是run_until_complete方法将协程包装成为了一个任务(task)对象。所谓task对象是Future类的子类。保存了协程运行后的状态,用于未来获取协程的结果。
例如:
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- loop = asyncio.get_event_loop()
- task = loop.create_task(fun())
- print(task)
- loop.run_until_complete(task)
- print(task)
创建task后,task在加入事件循环之前是pending状态,因为do_some_work中没有耗时的阻塞操作,task很快就执行完毕了。后面打印的finished状态。
asyncio.ensure_future 和 loop.create_task都可以创建一个task,run_until_complete的参数是一个futrue对象。当传入一个协程,其内部会自动封装成task,task是Future的子类。isinstance(task, asyncio.Future)将会输出True。
【三】绑定回调
在task执行完毕的时候可以获取执行的结果,回调的最后一个参数是future对象,通过该对象可以获取协程返回值。如果回调需要多个参数,可以通过偏函数导入。
例如:
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- def callback(future):
- print('Callback: ', future.result())
- loop = asyncio.get_event_loop()
- task = loop.create_task(fun())
- #print(task)
- task.add_done_callback(callback)
- loop.run_until_complete(task)
- #print(task)
也可以使用ensure_future获取返回值
例如:
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- #def callback(future):
- #print('Callback: ', future.result())
- loop = asyncio.get_event_loop()
- #task = loop.create_task(fun())
- #task.add_done_callback(callback)
- task = asyncio.ensure_future(fun())
- loop.run_until_complete(task)
- print('the fun() return is: {}'.format(task.result()))
【四】await阻塞
使用async可以定义协程对象,使用await可以针对耗时的操作进行挂起,就像生成器里的yield一样,函数让出控制权。协程遇到await,事件循环将会挂起该协程,执行别的协程,直到其他的协程也挂起或者执行完毕,再进行下一个协程的执行。
耗时的操作一般是一些IO操作,例如网络请求,文件读取等。我们使用asyncio.sleep函数来模拟IO操作。协程的目的也是让这些IO操作异步化。
例如:
- #coding:utf-8
- import asyncio
- import threading
- import time
- async def hello():
- print("hello 1")
- r = await asyncio.sleep(1)
- print("hello 2")
- def main():
- loop = asyncio.get_event_loop()
- print("begin")
- loop.run_until_complete(hello())
- loop.close()
- print("end")
- if __name__ == "__main__":
- main()
【五】3.6更新
①可以在同一个协程函数中同时使用await和yield
例如:
- import asyncio
- async def ticker(delay, to):
- for i in range(to):
- yield i
- await asyncio.sleep(delay)
- async def run():
- async for i in ticker(1, 10):
- print(i)
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(run())
- finally:
- loop.close()
顺带一提,yield 我们可以暂且认为是一种中断机制(详情可以参考官方文档,这种解释只是便于说明await)
例如:
- def a():
- print("first")
- yield
- print("second")
- yield
- print("end")
- yield
- if __name__ == "__main__":
- g1=a()
- print("next1")
- g1.__next__()
- print("next2")
- g1.__next__()
- print("next3")
- g1.__next__()
②允许在协程函数中异步推导式
例如:
- async def ticker(delay, to):
- for i in range(to):
- yield i
- await asyncio.sleep(delay)
- async def run():
- result = [i async for i in ticker(1, 10) if i%2]
- print(result)
- import asyncio
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(run())
- finally:
- loop.close()
【六】协程并发
定义tasks时可以设置多个ensure,也可以像多线程那样用append方法实现
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- for i in range(4, 6):
- tasks.append(asyncio.ensure_future(do_some_work(i)))
当遇到阻塞时可以使用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)
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(3)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- for i in range(4, 6):
- tasks.append(asyncio.ensure_future(do_some_work(i)))
- loop = asyncio.get_event_loop()
- start = now()
- loop.run_until_complete(asyncio.wait(tasks))
- for task in tasks:
- print('Task ret: ', task.result())
- print('TIME: ', now() - start)
通过运行时间可以看出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的返回值就是协程运行的结果。
- #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)
不在main协程函数里处理结果,直接返回await的内容,那么最外层的run_until_complete将会返回main协程的结果。
- 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)
- ]
- return await asyncio.gather(*tasks)
- start = now()
- loop = asyncio.get_event_loop()
- results = loop.run_until_complete(main())
- for result in results:
- print('Task ret: ', result)
- print('TIME: ', now() - start)
或者返回使用asyncio.wait方式挂起协程。
- 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)
- ]
- return await asyncio.wait(tasks)
- start = now()
- loop = asyncio.get_event_loop()
- done, pending = loop.run_until_complete(main())
- for task in done:
- print('Task ret: ', task.result())
- print('TIME: ', now() - start)
也可以使用asyncio的as_completed方法
- 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)
- ]
- for task in asyncio.as_completed(tasks):
- result = await task
- print('Task ret: {}'.format(result))
- start = now()
- loop = asyncio.get_event_loop()
- done = loop.run_until_complete(main())
- print('TIME: ', now() - start)
由此可见,协程的调用和组合十分的灵活,我们可以发挥想象尽情的浪
【八】协程停止
future对象有几个状态:
Pending
Running
Done
Cancelled
创建future的时候,task为pending,事件循环调用执行的时候当然就是running,调用完毕自然就是done,如果需要停止事件循环,就需要先把task取消。可以使用asyncio.Task获取事件循环的task
例如:
- 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)
- 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)
- ]
- start = now()
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(asyncio.wait(tasks))
- except KeyboardInterrupt as e:
- print(asyncio.Task.all_tasks())
- for task in asyncio.Task.all_tasks():
- print(task.cancel())
- loop.stop()
- loop.run_forever()
- finally:
- loop.close()
- print('TIME: ', now() - start)
启动事件循环之后,马上ctrl+c,会触发run_until_complete的执行异常 KeyBorardInterrupt。然后通过循环asyncio.Task取消future
True表示cannel成功,loop stop之后还需要再次开启事件循环,最后在close,不然会报错。
循环task,逐个cancel是一种方案,可是正如上面我们把task的列表封装在main函数中,main函数外进行事件循环的调用。这个时候,main相当于最外出的一个task,那么处理包装的main函数即可。
- 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)
- ]
- done, pending = await asyncio.wait(tasks)
- for task in done:
- print('Task ret: ', task.result())
- start = now()
- loop = asyncio.get_event_loop()
- task = asyncio.ensure_future(main())
- try:
- loop.run_until_complete(task)
- except KeyboardInterrupt as e:
- print(asyncio.Task.all_tasks())
- print(asyncio.gather(*asyncio.Task.all_tasks()).cancel())
- loop.stop()
- loop.run_forever()
- finally:
- loop.close()
【九】不同线程的事件循环
很多时候,我们的事件循环用于注册协程,而有的协程需要动态的添加到事件循环中。一个简单的方式就是使用多线程。当前线程创建一个事件循环,然后在新建一个线程,在新线程中启动事件循环。当前线程不会被block。
- import asyncio
- import time
- now = lambda: time.time()
- from threading import Thread
- def start_loop(loop):
- asyncio.set_event_loop(loop)
- loop.run_forever()
- def more_work(x):
- print('More work {}'.format(x))
- time.sleep(x)
- print('Finished more work {}'.format(x))
- start = now()
- new_loop = asyncio.new_event_loop()
- t = Thread(target=start_loop, args=(new_loop,))
- t.start()
- print('TIME: {}'.format(time.time() - start))
- new_loop.call_soon_threadsafe(more_work, 6)
- new_loop.call_soon_threadsafe(more_work, 3)
启动上述代码之后,当前线程不会被block,新线程中会按照顺序执行call_soon_threadsafe方法注册的more_work方法,后者因为time.sleep操作是同步阻塞的,因此运行完毕more_work需要大致6 + 3
【十】新线程协程
新线程协程的话,可以在主线程中创建一个new_loop,然后在另外的子线程中开启一个无限事件循环。主线程通过run_coroutine_threadsafe新注册协程对象。这样就能在子线程中进行事件循环的并发操作,同时主线程又不会被block。一共执行的时间大概在6s左右。
- import asyncio
- import time
- now = lambda: time.time()
- from threading import Thread
- def start_loop(loop):
- asyncio.set_event_loop(loop)
- loop.run_forever()
- async def do_some_work(x):
- print('Waiting {}'.format(x))
- await asyncio.sleep(x)
- print('Done after {}s'.format(x))
- def more_work(x):
- print('More work {}'.format(x))
- time.sleep(x)
- print('Finished more work {}'.format(x))
- start = now()
- new_loop = asyncio.new_event_loop()
- t = Thread(target=start_loop, args=(new_loop,))
- t.start()
- print('TIME: {}'.format(time.time() - start))
- asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop)
- asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop)