Search documentation

Type to search the documentation.

Docs/Examples/Process inputs in parallel

Python example

Process inputs in parallel

Map a task over a sequence with bounded concurrency and ordered results.

Before you run#

Use an installed, authenticated Yak CLI with access to the invite-only preview. Read Parallel Execution for the full setup and current behavior.

Source code#

Download parallel_task_map.py

Canonical exampleparallel_task_map.py
parallel_task_map.py
import asyncio

import yak

app = yak.App(name="parallel-task-map", image="python:3.11")


@app.task()
def square(value: int) -> int:
    return value * value


@app.entrypoint()
async def main() -> None:
    results = square.map.aio(range(1, 6), max_in_flight=3)
    try:
        async for result in results:
            print(result)
    finally:
        await results.aclose()


if __name__ == "__main__":
    asyncio.run(main())

Run the example#

Save the source as parallel_task_map.py, then run:

Commandparallel_task_map.py
parallel_task_map.py ready
$ yak run parallel_task_map.py