异步迭代器的类型是什么?

时间:2017-10-17 08:39:05

标签: python python-asyncio typing

示例(问题如下):

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)

1 个答案:

答案 0 :(得分:1)

typing模块的{p> Source code毫无疑问。

async def receive(self, topic: str) -> t.AsyncIterable:

async def receive(self, topic: str) -> t.AsyncIterator:

如果你确定它是严格的迭代器。