`

Python协程:从yield/send到async/await

阅读更多

Python由于众所周知的GIL的原因,导致其线程无法发挥多核的并行计算能力(当然,后来有了multiprocessing,可以实现多进程并行),显得比较鸡肋。既然在GIL之下,同一时刻只能有一个线程在运行,那么对于CPU密集的程序来说,线程之间的切换开销就成了拖累,而以I/O为瓶颈的程序正是协程所擅长的:

多任务并发(非并行),每个任务在合适的时候挂起(发起I/O)和恢复(I/O结束)

Python中的协程经历了很长的一段发展历程。其大概经历了如下三个阶段:

  1. 最初的生成器变形yield/send
  2. 引入@asyncio.coroutine和yield from
  3. 在最近的Python3.5版本中引入async/await关键字

从yield说起

先看一段普通的计算斐波那契续列的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def old_fib(n):
	res = [0] * n
	index = 0
	a = 0
	b = 1
	while index < n:
		res[index] = b
		a, b = b, a + b
		index += 1
	return res

print('-'*10 + 'test old fib' + '-'*10)
for fib_res in old_fib(20):
	print(fib_res)

 

如果我们仅仅是需要拿到斐波那契序列的第n位,或者仅仅是希望依此产生斐波那契序列,那么上面这种传统方式就会比较耗费内存。

这时,yield就派上用场了。

1
2
3
4
5
6
7
8
9
10
11
12
def fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		yield b
		a, b = b, a + b
		index += 1

print('-'*10 + 'test yield fib' + '-'*10)
for fib_res in fib(20):
	print(fib_res)

 

当一个函数中包含yield语句时,python会自动将其识别为一个生成器。这时fib(20)并不会真正调用函数体,而是以函数体生成了一个生成器对象实例。

yield在这里可以保留fib函数的计算现场,暂停fib的计算并将b返回。而将fib放入for…in循环中时,每次循环都会调用next(fib(20)),唤醒生成器,执行到下一个yield语句处,直到抛出StopIteration异常。此异常会被for循环捕获,导致跳出循环。

Send来了

从上面的程序中可以看到,目前只有数据从fib(20)中通过yield流向外面的for循环;如果可以向fib(20)发送数据,那不是就可以在Python中实现协程了嘛。

于是,Python中的生成器有了send函数,yield表达式也拥有了返回值。

我们用这个特性,模拟一个额慢速斐波那契数列的计算:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def stupid_fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		sleep_cnt = yield b
		print('let me think {0} secs'.format(sleep_cnt))
		time.sleep(sleep_cnt)
		a, b = b, a + b
		index += 1
print('-'*10 + 'test yield send' + '-'*10)
N = 20
sfib = stupid_fib(N)
fib_res = next(sfib)
while True:
	print(fib_res)
	try:
		fib_res = sfib.send(random.uniform(0, 0.5))
	except StopIteration:
		break

 

其中next(sfib)相当于sfib.send(None),可以使得sfib运行至第一个yield处返回。后续的sfib.send(random.uniform(0, 0.5))则将一个随机的秒数发送给sfib,作为当前中断的yield表达式的返回值。这样,我们可以从“主”程序中控制协程计算斐波那契数列时的思考时间,协程可以返回给“主”程序计算结果,Perfect!

yield from是个什么鬼?

yield from用于重构生成器,简单的,可以这么使用:

1
2
3
4
5
6
7
def copy_fib(n):
	print('I am copy from fib')
	yield from fib(n)
	print('Copy end')
print('-'*10 + 'test yield from' + '-'*10)
for fib_res in copy_fib(20):
	print(fib_res)

 

这种使用方式很简单,但远远不是yield from的全部。yield from的作用还体现可以像一个管道一样将send信息传递给内层协程,并且处理好了各种异常情况,因此,对于stupid_fib也可以这样包装和使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def copy_stupid_fib(n):
	print('I am copy from stupid fib')
	yield from stupid_fib(n)
	print('Copy end')
print('-'*10 + 'test yield from and send' + '-'*10)
N = 20
csfib = copy_stupid_fib(N)
fib_res = next(csfib)
while True:
	print(fib_res)
	try:
		fib_res = csfib.send(random.uniform(0, 0.5))
	except StopIteration:
		break

 

如果没有yield from,这里的copy_yield_from将会特别复杂(因为要自己处理各种异常)。

asyncio.coroutine和yield from

yield from在asyncio模块中得以发扬光大。先看示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@asyncio.coroutine
def smart_fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		sleep_secs = random.uniform(0, 0.2)
		yield from asyncio.sleep(sleep_secs)
		print('Smart one think {} secs to get {}'.format(sleep_secs, b))
		a, b = b, a + b
		index += 1

@asyncio.coroutine
def stupid_fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		sleep_secs = random.uniform(0, 0.4)
		yield from asyncio.sleep(sleep_secs)
		print('Stupid one think {} secs to get {}'.format(sleep_secs, b))
		a, b = b, a + b
		index += 1

if __name__ == '__main__':
	loop = asyncio.get_event_loop()
	tasks = [
		asyncio.async(smart_fib(10)),
		asyncio.async(stupid_fib(10)),
	]
	loop.run_until_complete(asyncio.wait(tasks))
	print('All fib finished.')
	loop.close()

 

asyncio是一个基于事件循环的实现异步I/O的模块。通过yield from,我们可以将协程asyncio.sleep的控制权交给事件循环,然后挂起当前协程;之后,由事件循环决定何时唤醒asyncio.sleep,接着向后执行代码。

这样说可能比较抽象,好在asyncio是一个由python实现的模块,那么我们来看看asyncio.sleep中都做了些什么:

1
2
3
4
5
6
7
8
9
10
@coroutine
def sleep(delay, result=None, *, loop=None):
    """Coroutine that completes after a given time (in seconds)."""
    future = futures.Future(loop=loop)
    h = future._loop.call_later(delay,
                                future._set_result_unless_cancelled, result)
    try:
        return (yield from future)
    finally:
        h.cancel()

 

首先,sleep创建了一个Future对象,作为更内层的协程对象,通过yield from交给了事件循环;其次,它通过调用事件循环的call_later函数,注册了一个回调函数。

通过查看Future类的源码,可以看到,Future是一个实现了__iter__对象的生成器:

1
2
3
4
5
6
7
8
9
class Future:
	#blabla...
    def __iter__(self):
        if not self.done():
            self._blocking = True
            yield self  # This tells Task to wait for completion.
        assert self.done(), "yield from wasn't used with future"
        return self.result()  # May raise too.

 

那么当我们的协程yield from asyncio.sleep时,事件循环其实是与Future对象建立了练习。每次事件循环调用send(None)时,其实都会传递到Future对象的__iter__函数调用;而当Future尚未执行完毕的时候,就会yield self,也就意味着暂时挂起,等待下一次send(None)的唤醒。

当我们包装一个Future对象产生一个Task对象时,在Task对象初始化中,就会调用Future的send(None),并且为Future设置好回调函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Task(futures.Future):
	#blabla...
    def _step(self, value=None, exc=None):
		#blabla...
        try:
            if exc is not None:
                result = coro.throw(exc)
            elif value is not None:
                result = coro.send(value)
            else:
                result = next(coro)
		#exception handle
        else:
            if isinstance(result, futures.Future):
                # Yielded Future must come from Future.__iter__().
                if result._blocking:
                    result._blocking = False
                    result.add_done_callback(self._wakeup)
		#blabla...

    def _wakeup(self, future):
        try:
            value = future.result()
        except Exception as exc:
            # This may also be a cancellation.
            self._step(None, exc)
        else:
            self._step(value, None)
        self = None  # Needed to break cycles when an exception occurs.

 

预设的时间过后,事件循环将调用Future._set_result_unless_cancelled:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Future:
	#blabla...
    def _set_result_unless_cancelled(self, result):
        """Helper setting the result only if the future was not cancelled."""
        if self.cancelled():
            return
        self.set_result(result)

    def set_result(self, result):
        """Mark the future done and set its result.

        If the future is already done when this method is called, raises
        InvalidStateError.
        """
        if self._state != _PENDING:
            raise InvalidStateError('{}: {!r}'.format(self._state, self))
        self._result = result
        self._state = _FINISHED
        self._schedule_callbacks()

 

这将改变Future的状态,同时回调之前设定好的Tasks._wakeup;在_wakeup中,将会再次调用Tasks._step,这时,Future的状态已经标记为完成,因此,将不再yield self,而return语句将会触发一个StopIteration异常,此异常将会被Task._step捕获用于设置Task的结果。同时,整个yield from链条也将被唤醒,协程将继续往下执行。

async和await

弄清楚了asyncio.coroutine和yield from之后,在Python3.5中引入的async和await就不难理解了:可以将他们理解成asyncio.coroutine/yield from的完美替身。当然,从Python设计的角度来说,async/await让协程表面上独立于生成器而存在,将细节都隐藏于asyncio模块之下,语法更清晰明了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
async def smart_fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		sleep_secs = random.uniform(0, 0.2)
		await asyncio.sleep(sleep_secs)
		print('Smart one think {} secs to get {}'.format(sleep_secs, b))
		a, b = b, a + b
		index += 1

async def stupid_fib(n):
	index = 0
	a = 0
	b = 1
	while index < n:
		sleep_secs = random.uniform(0, 0.4)
		await asyncio.sleep(sleep_secs)
		print('Stupid one think {} secs to get {}'.format(sleep_secs, b))
		a, b = b, a + b
		index += 1

if __name__ == '__main__':
	loop = asyncio.get_event_loop()
	tasks = [
		asyncio.ensure_future(smart_fib(10)),
		asyncio.ensure_future(stupid_fib(10)),
	]
	loop.run_until_complete(asyncio.wait(tasks))
	print('All fib finished.')
	loop.close()

 

想要继续弄清楚async/await和asyncio.coroutine/yield from的区别,可以看看这篇文章

总结


至此,Python中的协程就介绍完毕了。示例程序中都是以sleep为异步I/O的代表,在实际项目中,可以使用协程异步的读写网络、读写文件、渲染界面等,而在等待协程完成的同时,CPU还可以进行其他的计算。协程的作用正在于此。

相关代码可以在GitHub上找到https://github.com/yubo1911/saber/tree/master/coroutine

转载请注明出处: http://blog.guoyb.com/2016/07/03/python-coroutine/

分享到:
评论

相关推荐

    Python协程深入理解-光环大数据Python基础教程.pdf

    光环大数据--大数据培训&人工智能培训 http://hadoop.aura.cn 光环大数据 http://hadoop.aura.cn Python 协程深入理解 光环大数据 Python 基础教程 光环大数据 Python 培训了解到,从语法上来看,协程和生成器类似,...

    Python协程的实现原理及相关技术.docx

    Python协程的实现原理是基于生成器的迭代器协议以及async/await关键字。这篇文章将介绍Python协程的实现原理及相关技术,帮助读者深入理解Python协程的工作原理。 1. 生成器的迭代器协议 Python协程的实现原理是基于...

    Python协程 yield与协程greenlet简单用法示例

    本文实例讲述了Python协程 yield与协程greenlet简单用法。分享给大家供大家参考,具体如下: 协程 协程,又称微线程,纤程。英文名Coroutine。 协程是啥 协程是python个中另外一种实现多任务的方式,只不过比线程更...

    Python从使用线程到使用async/await的深入讲解

    为了简化并更好地标识异步IO,从Python 3.5开始引入了新的语法async和await,可以让coroutine的代码更简洁易读。 请注意,async和await是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换: 把@...

    python生成器/yield协程/gevent写简单的图片下载器功能示例

    主要介绍了python生成器/yield协程/gevent写简单的图片下载器功能,结合实例形式分析了python生成器、yield协程与gevent图片下载器相关功能定义与使用技巧,需要的朋友可以参考下

    详解Python 协程的详细用法使用和例子

    协程可能会从调用方接收数据,不过调用方把数据提供给协程使用的是 .send(datum) 方法,而不是next(…) 函数。 ==yield 关键字甚至还可以不接收或传出数据。不管数据如何流动, yield 都是一种流程控制工具,使用它...

    为什么你还不懂得怎么使用Python协程

    协程可能从调用方接受数据,调用方是通过send(datum)的方式把数据提供给协程使用,而不是next(…)函数,通常调用方会把值推送给协程。 协程可以把控制器让给中心调度程序,从而激活其他的协程 所以总体上在协程中...

    Python库 | pytest-yield-1.0.0.zip

    资源分类:Python库 所属语言:Python 资源全名:pytest-yield-1.0.0.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    维酷koa博客框架version0.4.3koajsblog.zip

    yield.js 测试 异步流 run.md 运行命令 实现功能 管理员设置栏目功能 文章发布功能 评论功能 信息提示页面 多用户注册登录 权限控制模型 404页面 建立模块主入口 bootstrap 缓存静态文件 安全性模块...

    sciter-sdk-4.0.3.5348

    [script] await/yield syntax compatibility with JS: await is allowed to be in async function() only, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function ...

    Python协程的用法和例子详解

    协程可能会从调用方接收数据,不过调用方把数据提供给协程使用的是 .send(datum) 方法,而不是next(…) 函数。 ==yield 关键字甚至还可以不接收或传出数据。不管数据如何流动, yield 都是一种流程控制工具,使用它...

    aliyun-cdn-sdk:阿里云 sdk

    aliyun-sdk阿里云 CDN API SDK初始化const co = require('co');const SDK = require('ali-cdn-sdk');...co(function* () { const ... const res = yield sdk.DescribeDomainHttpCodeData({ DomainName: 'a.alipayobjects.

    python协程用法实例分析

    本文实例讲述了python协程用法。分享给大家供大家参考。具体如下: 把函数编写为一个任务,从而能处理发送给他的一系列输入,这种函数称为协程 def print_matchs(matchtext): print looking for,matchtext while ...

    simpyder:超高速异步协程Python爬虫

    Simpyder - 轻量级协程Python爬虫 特点 轻量级:下载便利,依赖较少,使用简单。 协程:单线程,通过协程实现并发。 可定制:简单配置,适应各种爬取场合。 快速开始 下载 #使用pip3 pip3 install simpyder --user #...

    Python协程操作之gevent(yield阻塞,greenlet),协程实现多任务(有规律的交替协作执行)用法详解

    本文实例讲述了Python 协程操作之gevent(yield阻塞,greenlet),协程实现多任务(有规律的交替协作执行)用法。分享给大家供大家参考,具体如下: 实现多任务:进程消耗的资源最大,线程消耗的资源次之,协程消耗的...

    浅析python协程相关概念

    协程的历史说来话长,要从生成器开始讲起。 如果你看过我之前的文章python奇遇记:迭代器和生成器 ,对生成器的概念应该很了解。生成器节省内存,用的时候才生成结果。   # 生成器表达式 a = (x*x for x in range...

    Python 中由 yield 实现异步操作

    yield在python中初学时,觉得比较难理解。yield的作用: ①返回一个值、②接收调用者的参数 分析下面的代码: #!/usr/bin/env python3 # -*- coding:utf-8 -*- def consumer(): r = '' while True: n = yield r ...

    React/Redux应用使用Async/Await的方法

    本篇文章主要介绍了React/Redux应用使用Async/Await的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Global site tag (gtag.js) - Google Analytics