深刻理解python协程


概述

因为 cpu和 磁盘读写的 效率有很大的差距,每每cpu执行代码,而后遇到须要从磁盘中读写文件的操做,此时主线程会中止运行,等待IO操做完成后再继续进行,这要就致使cpu的利用率很是的低。服务器

协程能够实现单线程同时执行多个任务,可是须要本身手动的经过send函数和yield关键字配合来传递消息,asyncio模块可以自动帮咱们传递消息。app

python中协程主要经历了以下三个阶段dom

1)生成器变形 yield/send异步

2)asyncio.coroutine和yield fromsocket

3)async/await关键字async


生成器变形 yield/send

yield

Python中函数若是把return换成了yield,那么这个函数就再也不普通函数了,而是一个生成器函数

简单生成器示例:oop

def mygen(alist):   # define a generator
    while alist:
        c = alist.pop()
        yield c


lst = [1, 2, 3]
g = mygen(lst)  # get a generator object
print(g)        # <generator object mygen at 0x0000020225555F10>

while True:
    try:
        print(next(g))  # 3 2 1
    except StopIteration:
        break


生成器本质上也是迭代器,所以不只可使用next()取值,还可使用for循环取值编码

for item in g:
    print(item)     # 3 2 1


send

生成器函数最大的特色是能够接收一个外部传入的变量,并根据变量内容计算结果后返回,这个特色是根据send()函数实现的

send()函数使用示例:

def gen():
    value = 0
    while True:
        receive = yield value
        if receive == "Q" or receive == "q":
            break
        value = "got:%s" % receive


g = gen()
print(g.send(None))     # 第一个必须是None,不然会报错
print(g.send("hello~"))
print(g.send(123))
print(g.send([1, 2, 3]))


执行结果

0
got:hello~
got:123
got:[1, 2, 3]

注意:第一个send()里传入的变量必须是None,不然会报错TypeError: can't send non-None value to a just-started generator



这里最关键的一步就是receive = yield value,这一句实际上分为三步

1)向函数外抛出(返回)value

2)暂停,等待next()或send()恢复

3)将等号右边的表达式的值(这个值是传入的)赋值给receive



下面来梳理一下执行流程

1)经过g.send(None)或者next(g)启动生成器函数,并执行到第一个yield的位置

2)执行yield value,程序返回value,也就是0,以后暂停,等待下一个next()或send(),注意这时并无给receive赋值

3)gen返回value以后跳出,执行主程序里面的g.send("hello~"),执行这一句会传入"hello~",从以前暂停的位置继续执行,也就是赋值给receive,继续往下执行,value变成"got:hello~",而后判断while,执行到yield value,返回value,因此打印出"got:hello~",以后进入暂停,等待下一个send()激活

4)后续的g.send(123)执行流程相似,若是传入"q",gen会执行到break,整个函数执行完毕,会得StopIteration



从上面能够看出,在第一次send(None)启动生成器(执行1>2,一般第一次返回的值并无什么用)以后,对于外部的每一次send(),生成器的实际在循环中的运行顺序是3–>1–>2,也就是先获取值,而后do something,而后返回一个值,再暂停等待。


yield from

yield from是Python3.3引入的,先来看一段代码

def gen1():
    yield range(5)


def gen2():
    yield from range(5)


iter1 = gen1()
iter2 = gen2()

for item in iter1:
    print(item)

for item in iter2:
    print(item)


执行结果

range(0, 5)
0
1
2
3
4

从上面的示例能够看出来yield是将range这个可迭代对象直接返回,而yield from解析range对象,将其中每个item返回,yield from本质上等于

for item in iterable:
    yield item



注意yield from后面只能接可迭代对象



下面来看一个例子,咱们编写一个斐波那契数列函数

def fab(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a+b
        n += 1


f = fab(5)

fab不是一个普通函数,而是一个生成器。所以fab(5)并无执行函数,而是返回一个生成器对象,假设要在fab()的基础上实现一个函数,调用起始都要记录日志

def wrapper(func_iter):
    print("start")
    for item in func_iter:
        yield item
    print("end")


wrap = wrapper(fab(5))
for i in wrap:
    print(i)



下面使用yield from代替for循环

def wrapper(func_iter):
    print("start")
    yield from func_iter
    print("end")


wrap = wrapper(fab(5))
for i in wrap:
    print(i)

asyncio.coroutine和yield from

yield from在asyncio模块(python3.4引入)中得以发扬光大。以前都是手动的经过send函数和yield关键字配合来传递消息,如今当声明函数为协程后,咱们经过事件循环来调度协程

import asyncio, random


@asyncio.coroutine      # 将一个generator定义为coroutine
def smart_fib(n):
    i, a, b = 0, 0, 1
    while i < n:
        sleep_time = random.uniform(0, 0.2)
        yield from asyncio.sleep(sleep_time)    # 一般yield from后都是接的耗时操做
        print("smart take %s secs to get %s" % (sleep_time, b))
        a, b = b, a+b
        i += 1


@asyncio.coroutine
def stupid_fib(n):
    i, a, b = 0, 0, 1
    while i < n:
        sleep_time = random.uniform(0, 0.5)
        yield from asyncio.sleep(sleep_time)
        print("stupid take %s secs to get %s" % (sleep_time, b))
        a, b = b, a+b
        i += 1


if __name__ == '__main__':
    loop = asyncio.get_event_loop()     # 获取事件循环的引用
    tasks = [                           # 建立任务列表
        smart_fib(10),
        stupid_fib(10),
    ]
    loop.run_until_complete(asyncio.wait(tasks))    # wait会分别把各个协程包装进一个Task 对象。
    print("All fib finished")
    loop.close()


yield from语法可让咱们方便地调用另外一个generator。 本例中yield from后面接的asyncio.sleep()也是一个coroutine(里面也用了yield from),因此线程不会等待asyncio.sleep(),而是直接中断并执行下一个消息循环。当asyncio.sleep()返回时,线程就能够从yield from拿到返回值(此处是None),而后接着执行下一行语句。


asyncio是一个基于事件循环的实现异步I/O的模块。经过yield from,咱们能够将协程asyncio.sleep的控制权交给事件循环,而后挂起当前协程;以后,由事件循环决定什么时候唤醒asyncio.sleep,接着向后执行代码。
协程之间的调度都是由事件循环决定。



yield from asyncio.sleep(sleep_secs) 这里不能用time.sleep(1)由于time.sleep()返回的是None,它不是iterable,还记得前面说的yield from后面必须跟iterable对象(能够是生成器,迭代器)。



另外一个示例

import asyncio


@asyncio.coroutine
def wget(host):
    print('wget %s...' % host)
    connect = asyncio.open_connection(host, 80)  # 与要获取数据的网页创建链接
    # 链接中包含一个 reader和writer
    reader, writer = yield from connect  # 经过writer向服务器发送请求,经过reader读取服务器repnse回来的请求
    header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host  # 组装请求头信息
    writer.write(header.encode('utf-8'))  # 须要对请求头信息进行编码
    yield from writer.drain()  # 因为writer中有缓冲区,若是缓冲区没满不且drain的话数据不会发送出去
    while True:
        line = yield from reader.readline()  # 返回的数据放在了reader中,经过readline一行一行地读取数据
        if line == b'\r\n':  # 由于readline实际上已经把\r\n转换成换行了,而此时又出现\r\n说明之前有连续两组\r\n
            break  # 即\r\n\r\n,因此下面就是response body了
        print('%s header > %s' % (host, line.decode('utf-8').rstrip()))
    # Ignore the body, close the socket
    writer.close()
    # reader.close()   AttributeError: 'StreamReader' object has no attribute 'close'


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()


async和await

弄清楚了asyncio.coroutine和yield from以后,在Python3.5中引入的async和await就不难理解了,咱们使用的时候只须要把@asyncio.coroutine换成async,把yield from换成await就能够了。固然,从Python设计的角度来讲,async/await让协程表面上独立于生成器而存在,将细节都隐藏于asyncio模块之下,语法更清晰明了。

加入新的关键字 async ,能够将任何一个普通函数变成协程

一个简单的示例

import time, asyncio, random

async def mygen(alist):
    while alist:
        c = alist.pop()
        print(c)


lst = [1, 2, 3]
g = mygen(lst)
print(g)



执行结果

<coroutine object mygen at 0x00000267723FB3B8>      # 协程对象
sys:1: RuntimeWarning: coroutine 'mygen' was never awaited



能够看到,咱们在前面加上async,该函数就变成了一个协程,可是async对生成器是无效的

async def mygen(alist):
    while alist:
        c = alist.pop()
        yield c

lst = [1, 2, 3]
g = mygen(lst)
print(g)



执行结果

<async_generator object mygen at 0x000001540EF505F8>    # 并非协程对象



因此正常的协程是这样的

import time, asyncio, random

async def mygen(alist):
    while alist:
        c = alist.pop()
        print(c)
        await asyncio.sleep(1)


lst1 = [1, 2, 3]
lst2 = ["a", "b", "c"]
g1 = mygen(lst1)
g2 = mygen(lst2)



要运行协程,要用事件循环
在上面的代码下面加上:

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [
        c1,
        c2
    ]
    loop.run_until_complete(asyncio.wait(tasks))
    print("all finished")
    loop.close()



参考:

1)https://blog.csdn.net/soonfly/article/details/78361819

2)https://blog.csdn.net/weixin_40247263/article/details/82728437

相关文章
相关标签/搜索