hey all - new prefect user here! i'm trying to fig...
# ask-community
l
hey all - new prefect user here! i'm trying to figure out how to run a task across all combinations of three parameters. here's a toy example (in practice, some lists are returned from previous tasks). this runs but doesn't do what i want -- what i'd like to get is a run of
add(1, 10, 100)
,
add(1, 10, 200)
,
add(1, 10, 300)
,
add(1, 20, 100)
, etc, with combinations similar to the results of
itertools.product
in standard python
Copy code
@task
def add(x, y, z):
    return x + y + z

@task
def print_all(results):
    print(results)

with Flow('Run combinations') as flow:
    a = [1, 2, 3]
    b = [10, 20, 30]
    c = [100, 200, 300]

    results = add.map(a, b, c)
    print_all(results)

flow.run()
one possibility is to compute all the combos in standard python, which is totally acceptable if that's the right approach! a working example of how to do this when one of the lists is returned from a task would be useful.
a
@Luke Segars I think the easiest would be if you create a new task that returns a list of all the things to iterate over, and you can then pass it to mapping. Here is how you could do it:
Copy code
import itertools

from prefect import task, Flow
from prefect.executors import LocalDaskExecutor


@task
def generate_random_numbers():
    list_of_tuples = list(itertools.product((1, 2, 3), (10, 20, 30), (100, 200, 300)))
    return list_of_tuples


@task
def sum_all(x):
    return sum(x)


@task(log_stdout=True)
def print_results(res):
    print(res)


with Flow("mapping", executor=LocalDaskExecutor()) as flow:
    numbers = generate_random_numbers()
    result = sum_all.map(numbers)
    print_results(result)

if __name__ == "__main__":
    flow.run()
💯 1