示例(问题如下):
import asyncio
import typing as t
from aiokafka import AIOKafkaConsumer
class KafkaSimpleClient:
...
async def receive(self, topic: str) -> ???:
bootstrap_servers = ','.join(
'{}:{}'.format(host, port)
for host, port in self._bootstrap_servers
)
consumer = AIOKafkaConsumer(
loop=asyncio.get_event_loop(),
bootstrap_servers=bootstrap_servers,
metadata_max_age_ms=5000,
)
consumer.subscribe(pattern=topic)
await consumer.start()
return consumer
现在,我正在努力使用receive
的返回类型(它返回一些可以用async for x in y
迭代的东西。它是什么?它是一个等待的迭代器吗?它是一个迭代器吗?等待?或许完全不同于其他东西?
??? = t.Awaitable[t.Iterator]
??? = t.Iterator[t.Awaitable]
??? = (Something else)
答案 0 :(得分:1)
typing
模块的{p> Source code毫无疑问。
async def receive(self, topic: str) -> t.AsyncIterable:
或
async def receive(self, topic: str) -> t.AsyncIterator:
如果你确定它是严格的迭代器。