• yield, async


    yield实现斐波那契序列:

    import sys, time
    
    def fib():
        a,b,c = 0,1,0
        while True:
            yield c
            a,b = b, c
            c = a + b
            
    if __name__ == '__main__':
        fib_iter = fib()
        for i in range(int(sys.argv[1])):
            print(fib_iter.__next__())

    Send:

    send函数用来向fib_iter发送数据,这样数据就可以双向流动。我们尝试使用send来模拟一个比较慢的生成器,我们让它一秒钟生成一个数:

    import sys, time
    
    def fib():
        a,b,c = 0,1,0
        while True:
            sleep_sec = yield c
            time.sleep(sleep_sec)
            a,b = b, c
            c = a + b
    
    def fib1():
        index = 1
        a = 0
        b = 1
        while index:
            yield b
            a, b = b, a + b
            index += 1
            
    if __name__ == '__main__':
        fib_iter = fib()
        print(fib_iter.__next__())    #先执行一下,让它停留在yield
        for i in range(int(sys.argv[1])):
            result = fib_iter.send(1)   #
            print(result)

    yield from 是什么?

      在上面的yield中,我们通过for循环使用__next__()方法来获取下一个值,也就是说想要获取下一个值就要重新yield一下;yield from 它可以简化这个过程,看看实例:

    import sys, time
    
    def fib(n):
        a,b,c = 0,1,0
        while c < n:
            yield c
            a,b = b, c
            c = a + b
    
    def gener(n):
        yield from fib(n)
            
    if __name__ == '__main__':
        print(list(gener(5000)))

    执行结果:

    D:>python fib.py
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4
    181]

    yield from , send传递信息:

    import sys, time
    
    def fib(n):
        a,b,c = 0,1,0
        while c < n:       
            sleep_sec = yield c
            time.sleep(sleep_sec)
            a,b = b, c
            c = a + b
    
    def gener(n):
        yield from fib(n)
            
    if __name__ == '__main__':
        gen = gener(10)
        print(gen.send(None))
        print(gen.send(1))
        print(gen.send(1))

    执行结果:

    D:>python fib.py
    0
    1
    1

    asyncio 和 yield from:

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

    import sys, time, asyncio
    
    def fib(n):
        a,b,c = 0,1,0
        while c < n:  
            yield from asyncio.sleep(2)
            print('-->', c)
            a,b = b, c
            c = a + b
    
    def stupid_fib(n):
        a,b,c = 0,1,0
        while c < n:
            yield from asyncio.sleep(2)
            print('==>', c)
            a,b = b,c
            c = a + b
            
    if __name__ == '__main__':
        loop = asyncio.get_event_loop()
        tasks = [asyncio.async(fib(10)), asyncio.async(stupid_fib(10))]
        loop.run_until_complete(asyncio.wait(tasks))
        print('all task finished')
        loop.close()

    执行结果:

    D:>python fib.py
    --> 0
    ==> 0
    --> 1
    ==> 1
    --> 1
    ==> 1
    --> 2
    ==> 2
    --> 3
    ==> 3
  • 相关阅读:
    一个神秘现象引发对beego框架的思考
    利用golang优雅的实现单实例
    Go语言及Beego框架环境搭建
    go语言正则表达式
    爬取珍爱网后用户信息展示
    Django之modelform简介
    Django之分页功能
    Django之ORM操作
    selenium模拟鼠标操作
    Django之模型注册
  • 原文地址:https://www.cnblogs.com/huangxm/p/5784453.html
Copyright © 2020-2023  润新知