关于程序员:Python-异步-异步迭代器15

5次阅读

共计 3938 个字符,预计需要花费 10 分钟才能阅读完成。

动动发财的小手,点个赞吧!

迭代是 Python 中的基本操作。咱们能够迭代列表、字符串和所有其余构造。

Asyncio 容许咱们开发异步迭代器。咱们能够通过定义一个实现 __aiter__() 和 __anext__() 办法的对象来在 asyncio 程序中创立和应用异步迭代器。

1. 什么是异步迭代器

异步迭代器是一个实现了 __aiter__() 和 __anext__() 办法的对象。在咱们认真钻研异步迭代器之前,让咱们回顾一下经典迭代器。

1.1. Iterators

迭代器是实现特定接口的 Python 对象。具体来说,返回迭代器实例的 __iter__() 办法和使迭代器步进一个循环并返回值的 __next__() 办法。能够应用内置函数 next() 步进迭代器或应用 for 循环遍历迭代器。许多 Python 对象是可迭代的,最值得注意的是列表等容器。

1.2. Asynchronous Iterators

异步迭代器是实现特定接口的 Python 对象。异步迭代器必须实现 __aiter__() 和 __anext__() 办法。

  • __aiter__() 办法必须返回迭代器的一个实例。
  • __anext__() 办法必须返回一个步进迭代器的可期待对象。

异步迭代器只能在 asyncio 程序中步进或遍历,例如在协程中。

能够应用 anext() 内置函数步进异步迭代器,该函数返回执行迭代器一步的可期待对象,例如一次调用 __anext__() 办法。

能够应用“async for”表达式遍历异步迭代器,该表达式将在每次迭代时主动调用 anext() 并期待返回的 awaitable 以检索返回值。

2. 什么是“async for”循环?

async for 表达式用于遍历异步迭代器。它是一个异步的 for 循环语句。异步迭代器是产生可期待对象的迭代器。您可能还记得 awaitable 是能够期待的对象,例如协程或工作。

异步生成器将主动实现异步迭代器办法,容许它像异步迭代器一样被迭代。await for 表达式容许调用者遍历 awaitable 的异步迭代器并从每个迭代器中检索后果。

这与遍历汇合或期待对象列表(例如协程对象)不同,相同,必须应用预期的异步迭代器办法提供返回的期待对象。在外部,async for 循环将依据须要主动解析或期待每个可期待的调度协程。

因为它是一个 for 循环,所以它假设(只管不要求)每个被遍历的期待对象都会产生一个返回值。async for 循环必须在协程内应用,因为它在外部会应用只能在协程内应用的 await 表达式。async for 表达式可用于在协程中遍历异步迭代器。

...
# traverse an asynchronous iterator
async for item in async_iterator:
    print(item)

这不会并行执行 for 循环。asyncio 无奈在一个 Python 线程中一次执行多个协程。

相同,这是一个异步 for 循环。不同的是,执行 for 循环的协程会暂停并在外部期待每个 awaitable。在幕后,这可能须要安顿和期待协程,或者期待工作。咱们也能够在列表了解中应用 async for 表达式。

...
# build a list of results
results = [item async for item async_iterator]

这将构建异步迭代器的返回值列表。

3. 如何应用异步迭代器

在本节中,咱们将认真钻研如何在 asyncio 程序中定义、创立、步进和遍历异步迭代器。让咱们从如何定义异步迭代器开始。

  • 定义异步迭代器

咱们能够通过定义一个实现了 __aiter__() 和 __anext__() 办法的类来定义一个异步迭代器。这些办法通常在 Python 对象上定义。重要的是,因为 __anext__() 函数必须返回一个可期待对象,所以它必须应用“async def”表达式定义。迭代实现后,__anext__() 办法必须引发 StopAsyncIteration 异样。

# define an asynchronous iterator
class AsyncIterator():
    # constructor, define some state
    def __init__(self):
        self.counter = 0
 
    # create an instance of the iterator
    def __aiter__(self):
        return self
 
    # return the next awaitable
    async def __anext__(self):
        # check for no further items
        if self.counter >= 10:
            raise StopAsyncIteration
        # increment the counter
        self.counter += 1
        # return the counter value
        return self.counter

因为异步迭代器是一个协程,并且每个迭代器返回一个在 asyncio 事件循环中调度和执行的期待对象,所以咱们能够在迭代器的主体内执行和期待期待对象。

...
# return the next awaitable
async def __anext__(self):
    # check for no further items
    if self.counter >= 10:
        raise StopAsyncIteration
    # increment the counter
    self.counter += 1
    # simulate work
    await asyncio.sleep(1)
    # return the counter value
    return self.counter
  • 创立异步迭代器

要应用异步迭代器,咱们必须创立迭代器。这波及失常创立 Python 对象。

...
# create the iterator
it = AsyncIterator()

这将返回一个“异步迭代器”,它是“异步迭代器”的一个实例。

  • 迭代一个异步迭代器

能够应用 anext() 内置函数遍历迭代器的一步,就像应用 next() 函数的经典迭代器一样。后果是期待的可期待对象。

...
# get an awaitable for one step of the iterator
awaitable = anext(it)
# execute the one step of the iterator and get the result
result = await awaitable

这能够一步实现。

...
# step the async iterator
result = await anext(it)
  • 遍历异步迭代器

异步迭代器也能够应用“async for”表达式在循环中遍历,该表达式将主动期待循环的每次迭代。

...
# traverse an asynchronous iterator
async for result in AsyncIterator():
    print(result)

咱们还能够应用带有“async for”表达式的异步列表了解来收集迭代器的后果。

...
# async list comprehension with async iterator
results = [item async for item in AsyncIterator()]

4. 异步迭代器示例

咱们能够摸索如何应用“async for”表达式遍历异步迭代器。在此示例中,咱们将更新之前的示例,以应用“async for”循环遍历迭代器直至实现。

此循环将主动期待从迭代器返回的每个可期待对象,检索返回值,并使其在循环体内可用,以便在这种状况下能够报告它。这可能是异步迭代器最常见的应用模式。

# SuperFastPython.com
# example of an asynchronous iterator with async for loop
import asyncio
 
# define an asynchronous iterator
class AsyncIterator():
    # constructor, define some state
    def __init__(self):
        self.counter = 0
 
    # create an instance of the iterator
    def __aiter__(self):
        return self
 
    # return the next awaitable
    async def __anext__(self):
        # check for no further items
        if self.counter >= 10:
            raise StopAsyncIteration
        # increment the counter
        self.counter += 1
        # simulate work
        await asyncio.sleep(1)
        # return the counter value
        return self.counter
 
# main coroutine
async def main():
    # loop over async iterator with async for loop
    async for item in AsyncIterator():
        print(item)
 
# execute the asyncio program
asyncio.run(main())

运行示例首先创立 main() 协程并将其用作 asyncio 程序的入口点。main() 协程运行并启动 for 循环。

异步迭代器的一个实例被创立,循环应用 anext() 函数主动单步执行它以返回一个可期待对象。而后循环期待可期待对象并检索一个值,该值可用于报告它的循环体。而后反复这个过程,挂起 main() 协程,执行迭代器和挂起的一个步骤,而后复原 main() 协程,直到迭代器耗尽。

一旦迭代器的外部计数器达到 10,就会引发 StopAsyncIteration。这不会终止程序。相同,它由“async for”表达式预期和解决并中断循环。

这突出显示了如何应用 async for 表达式遍历异步迭代器。

1
2
3
4

本文由 mdnice 多平台公布

正文完
 0