python——asyncio模块实现协程、异步编程
我们都知道,现在的服务器开发对于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)
python——asyncio模块实现协程、异步编程的更多相关文章
- 运筹帷幄决胜千里,Python3.10原生协程asyncio工业级真实协程异步消费任务调度实践
我们一直都相信这样一种说法:协程是比多线程更高效的一种并发工作方式,它完全由程序本身所控制,也就是在用户态执行,协程避免了像线程切换那样产生的上下文切换,在性能方面得到了很大的提升.毫无疑问,这是颠扑 ...
- python 多协程异步IO爬取网页加速3倍。
from urllib import request import gevent,time from gevent import monkey#该模块让当前程序所有io操作单独标记,进行异步操作. m ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- python进阶——进程/线程/协程
1 python线程 python中Threading模块用于提供线程相关的操作,线程是应用程序中执行的最小单元. #!/usr/bin/env python # -*- coding:utf-8 - ...
- 12.python进程\协程\异步IO
进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...
- Python全栈开发-Day10-进程/协程/异步IO/IO多路复用
本节内容 多进程multiprocessing 进程间的通讯 协程 论事件驱动与异步IO Select\Poll\Epoll——IO多路复用 1.多进程multiprocessing Python ...
- Python 8 协程/异步IO
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- Python程序中的协程操作-gevent模块
目录 一.安装 二.Gevent模块介绍 2.1 用法介绍 2.2 例:遇到io主动切换 2.3 查看threading.current_thread().getName() 三.Gevent之同步与 ...
- 31、Python程序中的协程操作(greenlet\gevent模块)
一.协程介绍 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 对比操作系统控制线程的切换,用 ...
随机推荐
- 【读书笔记-数据挖掘概念与技术】数据仓库与联机分析处理(OLAP)
之前看了认识数据以及数据的预处理,那么,处理之后的数据放在哪儿呢?就放在一个叫“数据仓库”的地方. 数据仓库的基本概念: 数据仓库的定义——面向主题的.集成的.时变的.非易失的 操作数据库系统VS数据 ...
- 移动端rem自适应布局(切图)
本篇适用于初次使用rem为单位切图而无从下手的童鞋.核心是根据屏幕动态改变根元素字体大小,以达到适配各种屏幕.这只是一个拿来就用的教程.很多东西没有详细说明.不过对于快速做手机端切图很有帮助. 模板: ...
- JavaScript 浮点数陷阱及解法
众所周知,JavaScript 浮点数运算时经常遇到会 0.000000001 和 0.999999999 这样奇怪的结果,如 0.1+0.2=0.30000000000000004.1-0.9=0. ...
- jQuery的回调管理机制(三)
jQuery.when()方法是jQuery内部使用回调机制的范例. // 参数为多个方法,这些方法全部执行完成之后执行回调 when: function( subordinate /* , ..., ...
- 如何将Ubuntu左边的面板放到底部
直入主题,有些人不喜欢ubuntu默认的面板在左边(笔者就是~囧~),我还是喜欢将面板放入到桌面的底部,这样更符合自己的使用习惯,但是ubuntu默认是不支持的,需要通过配置工具来配置. 这个时候我们 ...
- Mavan学习之pom聚合
所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...
- JavaScript中Array
一,针对于数组 const arr = ['a','b','c','d']; Array.indexOf 将“返回第一次出现给定元素的索引”; console.log(arr.indexOf('b' ...
- LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)
题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description Problem: 已知两个有序链表(链表中的数 ...
- 记录一下使用Ubuntu16.0.4配置和使用docker registry
h1, h2, h3, h4, h5, h6, p, blockquote { margin: 5px; padding: 5; } body { font-family: "Helveti ...
- wordpress---page页面数据调用
在wordpress的开发中,会使用wordpress的的页面,那么页面数据该怎么调用呢? 拿到页面的 content: <?php if(have_posts()) : ?> <? ...