在同步IO中,线程启动一个IO操作然后就立即进入等待状态,直到IO操作完成后才醒来继续执行。而异步IO方式中,线程发送一个IO请求到内核,然后继续处理其他的事情,内核完成IO请求后,将会通知线程IO操作完成了。

如果IO请求需要大量时间执行的话,异步IO方式可以显著提高效率,因为在线程等待的这段时间内,CPU将会调度其他线程进行执行,如果没有其他线程需要执行的话,这段时间将会浪费掉。

协程

协程(Coroutine),又称微线程。

我们平常使用的函数又称子程序,是层级调用的,即A函数调用B,B函数又调用C,那么需要等C执行完毕返回,然后B程序执行完毕返回,最后A执行完毕。

协程看上去也是子程序,但是执行顺序和子程序不同:协程执行过程可以中断,同样是A函数调用B,但B可以执行一部分继续去执行A,然后继续执行B未执行完的部分。

协程看起来很像多线程。但协程最大的优势是极高的执行效率。因为线程需要互相切换,切换需要开销。且线程直接共享变量需要使用锁机制,因为协程只有一个线程,不存在同时写变量冲突。

Python对协程的支持是通过generator实现的。

在generator中,我们不但可以通过for循环来迭代,还可以不断调用next()函数获取由yield语句返回的下一个值。

但是Python的yield不但可以返回一个值,它还可以接收调用者发出的参数。下面是一个典型的生产者-消费者模型:

# coding: utf-8

def consumer():
r = ''
while True:
n = yield r
print('[Consumer] Consuming %s' % n)
r = '200 OK' def produce(c):
c.send(None) #启动生成器
i = 0
while i < 5:
i = i + 1
print('[Produce] Start produce %s' % i)
r = c.send(i)
print('[Produce] Consumer return %s' % r) c = consumer() #生成器
produce(c)

输出:

[Produce] Start produce 1
[Consumer] Consuming 1
[Produce] Consumer return 200 OK
[Produce] Start produce 2
[Consumer] Consuming 2
[Produce] Consumer return 200 OK
[Produce] Start produce 3
[Consumer] Consuming 3
[Produce] Consumer return 200 OK
[Produce] Start produce 4
[Consumer] Consuming 4
[Produce] Consumer return 200 OK
[Produce] Start produce 5
[Consumer] Consuming 5
[Produce] Consumer return 200 OK

执行顺序:

1、发送None启动生成器consumer(),运行yield r,返回'',程序中断;

2、第2次发送1,从上次运行结束的地方开始,先通过n = yield r接收到1,继续运行打印语句,再次运行到yield r,返回'200 OK';

3、第3次发送2,从上次运行结束的地方开始,先通过n = yield r接收到2,继续运行打印语句,再次运行到yield r,返回'200 OK';

4、...

大家可以使用 Intellij IDEA调试功能 进行单步运行观察执行流程。

整个流程由一个线程执行,produce和consumer协作完成任务,所以称为“协程”,而非线程的抢占式多任务。

asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持。使用asyncio可以实现单线程并发IO操作。

@asyncio.coroutine把一个generator标记为coroutine类型:

# coding: utf-8
import asyncio @asyncio.coroutine
def helloWorld(n):
print('Hello world! %s' % n)
r = yield from asyncio.sleep(3)
print('Hello %s %s ' % (r, n)) loop = asyncio.get_event_loop()
tasks = [helloWorld(1), helloWorld(2), helloWorld(3), helloWorld(4)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

输出:

Hello world! 2
Hello world! 3
Hello world! 1
Hello world! 4 #(等待3秒左右) Hello None 2
Hello None 1
Hello None 3
Hello None 4

程序先运行Hello world! ,然后由于asyncio.sleep()也是一个coroutine,线程不会等待asyncio.sleep(),而是直接中断并执行下一个消息循环。当asyncio.sleep()返回时,线程就可以从yield from拿到返回值(此处是None),然后接着执行下一行语句。

asyncio的编程模型就是一个消息循环。我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO。

async/await

用asyncio提供的@asyncio.coroutine可以把一个generator标记为coroutine类型,然后在coroutine内部用yield from调用另一个coroutine实现异步操作。

为了简化并更好地标识异步IO,从Python 3.5开始引入了新的语法asyncawait,可以让coroutine的代码更简洁易读。

请注意,asyncawait是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换:

  1. @asyncio.coroutine替换为async
  2. yield from替换为await

上节的代码用Python3.5写:

# coding: utf-8
import asyncio async def helloWorld(n):
print('Hello world! %s' % n)
r = await asyncio.sleep(3)
print('Hello %s %s ' % (r, n)) loop = asyncio.get_event_loop()
tasks = [helloWorld(1), helloWorld(2), helloWorld(3), helloWorld(4)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

aiohttp

aiohttp是基于asyncio实现的HTTP框架。

需要先安装:

$ pip install aiohttp

控制台输出:

Collecting aiohttp
Downloading aiohttp-1.3.1-cp34-cp34m-win32.whl (147kB)
100% |████████████████████████████████| 153kB 820kB/s
Collecting async-timeout>=1.1.0 (from aiohttp)
Downloading async_timeout-1.1.0-py3-none-any.whl
Collecting yarl>=0.8.1 (from aiohttp)
Downloading yarl-0.9.6-cp34-cp34m-win32.whl (77kB)
100% |████████████████████████████████| 81kB 1.8MB/s
Collecting chardet (from aiohttp)
Downloading chardet-2.3.0-py2.py3-none-any.whl (180kB)
100% |████████████████████████████████| 184kB 890kB/s
Collecting multidict>=2.1.4 (from aiohttp)
Downloading multidict-2.1.4-cp34-cp34m-win32.whl (133kB)
100% |████████████████████████████████| 143kB 3.3MB/s
Installing collected packages: async-timeout, multidict, yarl, chardet, aiohttp
Successfully installed aiohttp-1.3.1 async-timeout-1.1.0 chardet-2.3.0 multidict-2.1.4 yarl-0.9.6

说明安装完成。

示例:

# coding: utf-8
import asyncio
from aiohttp import web @asyncio.coroutine
def index(request):
return web.Response(body=b'Hello aiohttp!', content_type='text/html') @asyncio.coroutine
def user(request):
name = request.match_info['name']
body = 'Hello %s' % name
return web.Response(body=body.encode('utf-8'), content_type='text/html')
pass @asyncio.coroutine
def init(loop):
# 创建app
app = web.Application(loop=loop) #添加路由
app.router.add_route('GET', '/', index)
app.router.add_route('GET', '/user/{name}', user) #运行server
server = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9999)
print('Server is running at http://127.0.0.1:9999 ...') loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

运行程序:

$ python user_aiohttp.py

Server is running at http://127.0.0.1:9999 ...

浏览器依次输入查看效果:

http://127.0.0.1:9999/

http://127.0.0.1:9999/user/aiohttp

更多知识可以查看aiohttp文档:

http://aiohttp.readthedocs.io/en/stable/

参考:

1、Python asyncio库的学习和使用

http://www.cnblogs.com/rockwall/p/5750900.html

2、异步IO

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143208573480558080fa77514407cb23834c78c6c7309000

Python学习--22 异步I/O的更多相关文章

  1. python学习(22) 访问数据库

    原文链接:http://www.limerence2017.com/2018/01/11/python22/ 本文介绍python如何使用数据库方面的知识. SQLite SQLite是一种嵌入式数据 ...

  2. python学习笔记 异步asyncio

    asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要 ...

  3. python学习22之函数式编程

    '''''''''1.高阶函数:将函数作为参数传递到另一个函数中,作为这个函数的参数,这样的方式叫高阶函数(1)map:两个参数,一个是函数,一个是iterator,将函数依次作用于Iterator中 ...

  4. iOS 学习 - 22 异步解析 JSON,使用 FMDB 存储,TableView 显示

    前提是已经知道了有哪些 key 值 Model 类: .h @interface ListModel : NSObject @property (nonatomic, copy)NSString *t ...

  5. Python学习(22):模块

    转自 http://www.cnblogs.com/BeginMan/p/3183656.html 一.模块基础 1.模块 自我包含,且有组织的代码片段就是模块 模块是Pyhon最高级别的程序组织单元 ...

  6. Python学习-22.Python中的函数——type

    type函数可以检测任何值或变量的类型. 例子: def printType(var): print(type(var)) class TestClass: pass printType(1) pri ...

  7. python学习-22 字符串格式化

    格式化包括:百分号方式和format方式 1.百分号 - %s   (%.4s   表示截取了4个字符) 传单个值: 例如: print('i am %s sex boy is ljj'%123) 运 ...

  8. Python学习---Python的异步IO[all]

    1.1.1. 前期环境准备和基础知识 安装: pip3 install aiohttp pip3 install grequests pip3 install wheel pip3 install s ...

  9. Python 学习教程汇总

    Python快速教程http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html简明Python教程https://bop.molun.ne ...

随机推荐

  1. 13、手把手教你Extjs5(十三)模块字段和Grid列的定义[1]

    这一节加入模块自定义字段,并根据这些字段生成model.然后再定义grid中的分组和列.从这一切开始真正进入到了模块自定义的节奏当中,代码的复杂度和技巧性也大大提高.先从模块字段的自定义开始.先看一下 ...

  2. MapReduce 表连接

    题目描述: 根据给定的关系 child parent Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy Ben Jack Alice Jack ...

  3. github上forck一个分支之后,如何和主分支同步

    github forck一个分之后,如果过一段时间就会和主分支的差异比较大. 这样提交pr的时候 就会冲突,这个时候我们就需要和主分支同步代码 git remote add upstream git@ ...

  4. ORACLE odbc驱动相关

    驱动下载 http://www.oracle.com/technetwork/topics/winsoft-085727.html http://www.oracle.com/technetwork/ ...

  5. Adaptive Server Enterprise ODBC driver connection strings

    Adaptive Server Enterprise 15.0 Driver={Adaptive Server Enterprise};app=myAppName;server=myServerAdd ...

  6. stm8的独立看门狗与窗口看门狗

    STM8拥有两个硬件看门狗,分别叫做独立看门狗和窗口看门狗 独立看门狗的框图如下 我们可以看到,独立看门狗的时钟来自于LSI内部低速振荡器,经过二分频到达看门狗外设单元,在经过一个七位的预分频到达计数 ...

  7. “&”详解

    1.引用 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. &作为引用的时候,必须在定义时候就进行初始化,若不进行初始化则会编译报错. 2.取地址 &作为取地 ...

  8. UVa 324 - Factorial Frequencies

    题目大意:给一个数n,统计n的阶乘中各个数字出现的次数.用java的大数做. import java.io.*; import java.util.*; import java.math.*; cla ...

  9. CocoaPods 更新慢&swift版本适配

    一.更新慢的问题 使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的 ...

  10. layout_toLeftOf = “@id/XX” 提示找不到该id的控件

    调布局的时候,需要把一个控件A放在另一个控件B的左边,我的xml布局文件是按照顺序从左到右定义的控件,所以先定义的控件A,然后控件B,在控件A的属性中,定义 android:layout_toLeft ...