aiter() 函数¶
aiter() 函数从异步可迭代对象中返回一个异步迭代器。
复杂度参考¶
| 操作 | 时间 | 空间 | 备注 |
|---|---|---|---|
aiter() |
O(1) | O(1) | 另加一次 __aiter__() 调用,并返回其结果;参数没有 __aiter__() 或结果没有 __anext__() 时引发 TypeError |
异步生成器的 __aiter__() |
O(1) | O(1) | 返回生成器自身 |
基本用法¶
创建异步迭代器¶
import asyncio
async def async_generator():
for i in range(3):
yield i
async def main():
# Get the async iterator - O(1): an async generator is its own
async_iter = aiter(async_generator())
# Iterate
value = await async_iter.__anext__()
print(value) # 0
asyncio.run(main())
从异步可迭代对象获取¶
import asyncio
class AsyncIterable:
def __aiter__(self):
return AsyncIterator()
class AsyncIterator:
def __init__(self):
self.count = 0
def __aiter__(self):
return self
async def __anext__(self):
if self.count >= 3:
raise StopAsyncIteration
self.count += 1
return self.count
async def main():
# Get iterator from iterable - O(1)
iterator = aiter(AsyncIterable())
# Use with async for
async for value in iterator:
print(value) # 1, 2, 3
asyncio.run(main())
配合 async for 使用¶
import asyncio
async def fetch_data(urls):
"""Simulate async data fetching."""
for url in urls:
await asyncio.sleep(0.1)
yield f"Data from {url}"
async def main():
urls = ["url1", "url2", "url3"]
# aiter is implicit in async for
async for data in fetch_data(urls):
print(data)
asyncio.run(main())
与 iter() 的比较¶
import asyncio
# iter() - synchronous iterator - O(1)
def sync_generator():
yield 1
yield 2
sync_iter = iter(sync_generator())
print(next(sync_iter)) # Works synchronously
# aiter() - asynchronous iterator - O(1)
async def async_generator():
yield 1
yield 2
async def main():
async_iter = aiter(async_generator())
print(await async_iter.__anext__()) # Must be awaited
asyncio.run(main())
相关函数¶
- anext() 函数 - 从异步迭代器获取下一个元素
- iter() 函数 - 同步迭代器
- next() 函数 - 获取下一个同步元素