From e2663c744cdb2d853e3df4eb427c4c25e21159b7 Mon Sep 17 00:00:00 2001 From: JJJZXY <1522844911@qq.com> Date: Sun, 17 Jan 2021 21:45:04 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E7=AC=AC=E4=BA=8C=E6=9C=9F=E8=AE=AD?= =?UTF-8?q?=E7=BB=83=E8=90=A5/5=E7=8F=AD/5=E7=8F=AD=5F=E5=BC=A0=E5=85=B4?= =?UTF-8?q?=E9=9B=A8/=E7=AC=AC=E4=BA=94=E5=91=A8=E4=BD=9C=E4=B8=9A=2001.01?= =?UTF-8?q?1-01.17/=E7=AC=AC=E4=B8=89=E8=8A=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\254\344\270\211\350\212\202" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\345\274\240\345\205\264\351\233\250/\347\254\254\344\272\224\345\221\250\344\275\234\344\270\232 01.011-01.17/\347\254\254\344\270\211\350\212\202" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\345\274\240\345\205\264\351\233\250/\347\254\254\344\272\224\345\221\250\344\275\234\344\270\232 01.011-01.17/\347\254\254\344\270\211\350\212\202" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\345\274\240\345\205\264\351\233\250/\347\254\254\344\272\224\345\221\250\344\275\234\344\270\232 01.011-01.17/\347\254\254\344\270\211\350\212\202" new file mode 100644 index 00000000..a52ff41c --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\345\274\240\345\205\264\351\233\250/\347\254\254\344\272\224\345\221\250\344\275\234\344\270\232 01.011-01.17/\347\254\254\344\270\211\350\212\202" @@ -0,0 +1,61 @@ +""" +笔记 +https://blog.csdn.net/weixin_53815762/article/details/112754992 + +什么是迭代器? +迭代器可以用来表示一个数据流,提供了数据的惰性返回功能(只有调用 next 方法才能返回值) + +什么是生成器? +生成器是一个特殊的迭代器,在迭代器的数据的惰性返回功能基础上,提供了额外的功能,实现了程序的暂停 + +迭代器和生成器的异同点 +都提供了惰性的返回功能,迭代器侧重于提供数据的惰性返回功能, 生成器侧重提供指令的惰性返回功能 + +协程的实现原理 +协程的实现原理就是生成器的实现原理,在生成器的基础上,又提供了传递值得功能 + +asyncio的实现原理 +自动维护了一个事件队列,然后循环访问事件来完成异步的消息维护 + +用协程实现一个计算平均数的函数 + +编写一个 asyncio 异步程序 + +扩展:了解 aiohttp 异步请求网址 + +""" +# 用协程实现一个计算平均数的函数 +def avg(): + total = 0 + length = 0 + while True: + try: + value = yield total/length + except ZeroDivisionError: + value = yield 0 + total += value + length += 1 + +my_avg = avg() +print(next(my_avg)) +print(my_avg.send(2)) +print(my_avg.send(3)) + +# 编写一个 asyncio 异步程序 +import asyncio +import time + +async def time_now(): + await asyncio.sleep(1) + print(f'now time is: {time.time()}') + +loop = asyncio.get_event_loop() +task_array = [] +for i in range(5): + task_array.append(time_now()) + +loop.run_until_complete(asyncio.wait(task_array)) +loop.close() + + + -- Gitee