zoukankan      html  css  js  c++  java
  • 轻松理解 Python 中的 async await 概念

    参考:https://blog.csdn.net/Likianta/article/details/90123678

    前言

    写这篇文章是受 xinghun85 的这篇博客 的启发, 但是人家后面写的东西跳跃太快, 有点没看懂, 自己在此做一个补充.

    我希望能用一个最平易近人的例子, 把 Python 协程中的 async/await 概念讲清楚, 希望能够帮助大家有一个形象化的认识.


    注: 所有的讲解都在代码的注释里.

    from time import sleep, time
    
    
    def demo1():
        """
        假设我们有三台洗衣机, 现在有三批衣服需要分别放到这三台洗衣机里面洗.
        """
        
        def washing1():
            sleep(3)  # 第一台洗衣机, 需要洗3秒才能洗完 (只是打个比方)
            print('washer1 finished')  # 洗完的时候, 洗衣机会响一下, 告诉我们洗完了
        
        def washing2():
            sleep(2)
            print('washer2 finished')
        
        def washing3():
            sleep(5)
            print('washer3 finished')
        
        washing1()
        washing2()
        washing3()
        
        """
        这个还是很容易理解的, 运行 demo1(), 那么需要10秒钟才能把全部衣服洗完.
        没错, 大部分时间都花在挨个地等洗衣机上了.
        """
    
    
    def demo2():
        """
        现在我们想要避免无谓的等待, 为了提高效率, 我们将使用 async.
        washing1/2/3() 本是 "普通函数", 现在我们用 async 把它们升级为 "异步函数".
        
        注: 一个异步的函数, 有个更标准的称呼, 我们叫它 "协程" (coroutine).
        """
        
        async def washing1():
            sleep(3)
            print('washer1 finished')
        
        async def washing2():
            sleep(2)
            print('washer2 finished')
        
        async def washing3():
            sleep(5)
            print('washer3 finished')
        
        washing1()
        washing2()
        washing3()
        
        """
        从正常人的理解来看, 我们现在有了异步函数, 但是却忘了定义应该什么时候 "离开" 一台洗衣
        机, 去看看另一个... 这就会导致, 现在的情况是我们一边看着第一台洗衣机, 一边着急地想着
        "是不是该去开第二台洗衣机了呢?" 但又不敢去 (只是打个比方), 最终还是花了10秒的时间才
        把衣服洗完.
        
        PS: 其实 demo2() 是无法运行的, Python 会直接警告你:
            RuntimeWarning: coroutine 'demo2.<locals>.washing1' was never awaited
            RuntimeWarning: coroutine 'demo2.<locals>.washing2' was never awaited
            RuntimeWarning: coroutine 'demo2.<locals>.washing3' was never awaited
        """
    
    
    def demo3():
        """
        现在我们吸取了上次的教训, 告诉自己洗衣服的过程是 "可等待的" (awaitable), 在它开始洗衣服
        的时候, 我们可以去弄别的机器.
        """
        
        async def washing1():
            await sleep(3)  # 注意这里加入了 await
            print('washer1 finished')
        
        async def washing2():
            await sleep(2)
            print('washer2 finished')
        
        async def washing3():
            await sleep(5)
            print('washer3 finished')
        
        washing1()
        washing2()
        washing3()
        
        """
        尝试运行一下, 我们会发现还是会报错 (报错内容和 demo2 一样). 这里我说一下原因, 以及在
        demo4 中会给出一个最终答案:
            1. 第一个问题是, await 后面必须跟一个 awaitable 类型或者具有 __await__ 属性的
            对象. 这个 awaitable, 并不是我们认为 sleep() 是 awaitable 就可以 await 了,
            常见的 awaitable 对象应该是:
                await asyncio.sleep(3)  # asyncio 库的 sleep() 机制与 time.sleep() 不
                # 同, 前者是 "假性睡眠", 后者是会导致线程阻塞的 "真性睡眠"
                await an_async_function()  # 一个异步的函数, 也是可等待的对象
            以下是不可等待的:
                await time.sleep(3)
                x = await 'hello'  # <class 'str'> doesn't define '__await__'
                x = await 3 + 2  # <class 'int'> dosen't define '__await__'
                x = await None  # ...
                x = await a_sync_function()  # 普通的函数, 是不可等待的
                
            2. 第二个问题是, 如果我们要执行异步函数, 不能用这样的调用方法:
                washing1()
                washing2()
                washing3()
            而应该用 asyncio 库中的事件循环机制来启动 (具体见 demo4 讲解).
        """
    
    
    def demo4():
        """
        这是最终我们想要的实现.
        """
        import asyncio  # 引入 asyncio 库
        
        async def washing1():
            await asyncio.sleep(3)  # 使用 asyncio.sleep(), 它返回的是一个可等待的对象
            print('washer1 finished')
        
        async def washing2():
            await asyncio.sleep(2)
            print('washer2 finished')
        
        async def washing3():
            await asyncio.sleep(5)
            print('washer3 finished')
        
        """
        事件循环机制分为以下几步骤:
            1. 创建一个事件循环
            2. 将异步函数加入事件队列
            3. 执行事件队列, 直到最晚的一个事件被处理完毕后结束
            4. 最后建议用 close() 方法关闭事件循环, 以彻底清理 loop 对象防止误用
        """
        # 1. 创建一个事件循环
        loop = asyncio.get_event_loop()
        
        # 2. 将异步函数加入事件队列
        tasks = [
            washing1(),
            washing2(),
            washing3(),
        ]
        
        # 3. 执行事件队列, 直到最晚的一个事件被处理完毕后结束
        loop.run_until_complete(asyncio.wait(tasks))
        """
        PS: 如果不满意想要 "多洗几遍", 可以多写几句:
            loop.run_until_complete(asyncio.wait(tasks))
            loop.run_until_complete(asyncio.wait(tasks))
            loop.run_until_complete(asyncio.wait(tasks))
            ...
        """
        
        # 4. 如果不再使用 loop, 建议养成良好关闭的习惯
        # (有点类似于文件读写结束时的 close() 操作)
        loop.close()
        
        """
        最终的打印效果:
            washer2 finished
            washer1 finished
            washer3 finished
            elapsed time = 5.126561641693115
            	(毕竟切换线程也要有点耗时的)
            
        说句题外话, 我看有的博主的加入事件队列是这样写的:
            tasks = [
                loop.create_task(washing1()),
                loop.create_task(washing2()),
                loop.create_task(washing3()),
            ]
            运行的效果是一样的, 暂不清楚为什么他们这样做.
        """
    
    
    if __name__ == '__main__':
        # 为验证是否真的缩短了时间, 我们计个时
        start = time()
        
        # demo1()  # 需花费10秒
        # demo2()  # 会报错: RuntimeWarning: coroutine ... was never awaited
        # demo3()  # 会报错: RuntimeWarning: coroutine ... was never awaited
        demo4()  # 需花费5秒多一点点
        
        end = time()
        print('elapsed time = ' + str(end - start))
     
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192

    参考

     
  • 相关阅读:
    数据库常用面试题
    Base64、Md5、Des加密
    C#反射类型的使用
    C# 反射工厂模式的实现
    生成验证码的例子
    轻松实现页面提交时,显示“提交中..” (转)
    编写高性能 Web 应用程序的 10 个技巧 (转)
    由Duwamish学习web.config的配置(转)
    .Net配置文件常用配置说明 (转)
    用asp.net还原与恢复sqlserver数据库(转)
  • 原文地址:https://www.cnblogs.com/kuangke/p/14702545.html
Copyright © 2011-2022 走看看