摘要: ''' 异步迭代器 ''' import asyncio class MyRange: def __init__(self, total=0): self.total = total self.count = 0 def __aiter__(self): return self async def 阅读全文
posted @ 2023-03-31 10:40 LeoShi2020 阅读(40) 评论(0) 推荐(0) 编辑
摘要: ''' 异步上下文管理 ''' import asyncio class ContextManager: def __init__(self): self.conn = None async def action(self): return self.conn async def __aenter_ 阅读全文
posted @ 2023-03-31 10:34 LeoShi2020 阅读(24) 评论(0) 推荐(0) 编辑
摘要: ''' 同步上下文管理器 ''' import time class ContextManager: def __init__(self): self.conn = None def action(self): return self.conn def __enter__(self): # 链接数据 阅读全文
posted @ 2023-03-31 10:25 LeoShi2020 阅读(13) 评论(0) 推荐(0) 编辑
摘要: import asyncio import time from concurrent.futures import ThreadPoolExecutor def download_img(url): print(f"下载图片:{url}") time.sleep(1) print(f"下载完成:{u 阅读全文
posted @ 2023-03-31 10:12 LeoShi2020 阅读(87) 评论(0) 推荐(0) 编辑
摘要: import time from concurrent.futures import ThreadPoolExecutor def download_img(url): print(f"下载图片:{url}") time.sleep(1) print(f"下载完成:{url}") def main( 阅读全文
posted @ 2023-03-31 10:08 LeoShi2020 阅读(29) 评论(0) 推荐(0) 编辑
摘要: ''' concurrent.futures 模块提供异步执行可调用对象高层接口,使用线程池 ThreadPoolExecutor 或 进程池 ProcessPoolExecutor 来实现异步。目的是保证服务稳定运行的前提下提供最大的并发能力。 ''' from concurrent.future 阅读全文
posted @ 2023-03-31 09:20 LeoShi2020 阅读(17) 评论(0) 推荐(0) 编辑
摘要: import asyncio from asyncio import Future async def f1(): print(1) await asyncio.sleep(3) print(2) return "f1" def callback(f: Future): f.get_loop().s 阅读全文
posted @ 2023-03-31 09:09 LeoShi2020 阅读(55) 评论(0) 推荐(0) 编辑