1、参考来源
https://docs.python.org/zh-cn/3.9/reference/datamodel.html?highlight=aiter#asynchronous-iterators
2、代码示例:
1 # -*- coding: utf-8 -*- 2 """ 3 File Name : test 4 Description : 5 Author : Administrator 6 """ 7 import asyncio 8 9 10 class Reader(object): 11 12 def __init__(self): 13 self.count = 0 14 15 async def readline(self): 16 self.count += 1 17 if self.count == 100: 18 return None 19 return self.count 20 21 def __aiter__(self): 22 return self 23 24 async def __anext__(self): 25 value = await self.readline() 26 if value == None: 27 raise StopAsyncIteration 28 return value 29 30 31 async def task_func(): 32 reader_obj = Reader() 33 async for value in reader_obj: 34 print(value) 35 36 37 if __name__ == '__main__': 38 asyncio.run(task_func())
3、运行结果
输出1-99的数字,这里忽略结果
标签:__,异步,return,迭代,Python,self,value,async,def From: https://www.cnblogs.com/ygbh/p/16726013.html