Creating a Map Task

A Map Task is a specialized task that enables the parallel execution of a single task across multiple inputs. It allows you to apply a task to each element of an input collection (like a list or array) and execute them in parallel. This is particularly useful when you need to perform the same operation on a large dataset or a collection of items, and you want to distribute the workload across multiple tasks

Here’s a simple example of how you might define and use a map task

from truefoundry.workflow import task, workflow, map_task, PythonTaskConfig, TaskPythonBuild


task_config = PythonTaskConfig(image=TaskPythonBuild(
        python_version="3.11",
        pip_packages=["truefoundry[workflow]"],
    ),
    service_account="<service-account>"
)

@task(task_config=cpu_task_config)
def square(x: int) -> int:
    return x * x

@workflow
def my_map_workflow(numbers: list[int]) -> list[int]:
    return map_task(square)(numbers=numbers)

# Example usage
result = my_map_workflow(numbers=[1, 2, 3, 4, 5])
# result will be [1, 4, 9, 16, 25]

In this example:

  • square: This is a simple task that squares a given integer.
  • map_task: The map_task function is used to apply the square task to each element of the numbers list.
  • my_map_workflow: This workflow demonstrates how to use the map task to process a list of numbers in parallel.

Map task on ui