On this page
  1. asyncio.as_completed
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

asyncio.as_completed

Prev: asyncio.all_tasks | Up: asyncio | Next: asyncio.Barrier
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.Barrier
Ref: docs.python.org — asyncio.as_completed
Tags: #public #python3 #stdlib #asyncio


Import

import asyncio
from asyncio import as_completed

Signature & Overview

asyncio.as_completed(fs, *, timeout=None)

Create an iterator of awaitables or their results in completion order.

Usage

import asyncio

async def work(delay: float, label: str) -> str:
    await asyncio.sleep(delay)
    return label

async def main():
    order = []
    for coro in asyncio.as_completed([work(0.02, "slow"), work(0.0, "fast")]):
        order.append(await coro)
    assert order == ["fast", "slow"]

asyncio.run(main())

Gotchas

REPL Inspect

from asyncio import as_completed
help(as_completed)

Linked from

Related notes