ancient-wolf-19325
08/01/2024, 9:59 PMDoubleTask for each integer. The SumTask collects the results from each DoubleTask and sums them up.
# Answer
To create a workflow that takes a list of integers, doubles each integer using a DoubleTask, and then sums the results using a SumTask, you can use Flyte's map_task to handle the parallel execution of the DoubleTask and then a regular task to sum the results. Here's how you can do it:
1. Define the DoubleTask: This task will double an integer.
2. Define the SumTask: This task will sum a list of integers.
3. Create the workflow: Use map_task to apply DoubleTask to each integer in the list and then use SumTask to sum the results.
Here's the code:
from flytekit import task, workflow, map_task
# Define the DoubleTask
@task
def double_task(x: int) -> int:
return x * 2
# Define the SumTask
@task
def sum_task(numbers: list[int]) -> int:
return sum(numbers)
# Create the workflow
@workflow
def double_and_sum_workflow(numbers: list[int]) -> int:
doubled_numbers = map_task(double_task)(x=numbers)
return sum_task(numbers=doubled_numbers)
# Example usage
if __name__ == "__main__":
result = double_and_sum_workflow(numbers=[1, 2, 3, 4, 5])
print(f"Result: {result}")
Explanation:
1. DoubleTask: This task takes an integer and returns its double.
2. SumTask: This task takes a list of integers and returns their sum.
3. Workflow: The workflow uses map_task to apply DoubleTask to each integer in the input list. The results are then passed to SumTask to get the final sum.
This approach leverages Flyte's ability to handle parallel execution efficiently using map_task.
Would you like more details on any specific part of this workflow?
Sources:
- Map Tasks
Slack Link
https://flyte-org.slack.com/archives/CP2HDHKE1/p1722549563.702889 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.