https://prefect.io logo
Title
k

Kyle McEntush

12/24/2022, 7:19 PM
I hope this is a relatively simple question: using prefect 2.0, how can I get a task to return two values? as a function it is something like
@task
def f(val):
   return val * 2, val * 4
then the call in my flow looks like
a, b = f.map([1, 2, 3])
but I get ValueError: not enough values to unpack (expected 2, got 1) I have confirmed that the code works as expected when not decorated with mapping over
@task
(that is, two inputs are returned)
1
r

Ryan Peden

12/24/2022, 7:40 PM
The task returns the tuple you expect it to so just calling the task would work:
# in a flow
# should set a = 2 and b = 4
a, b = f(1)
But as you mentioned,
map
runs the task once for each of the input values, and you get back a list of futures. Each future will resolve to a tuple returned by one of the task runs. Running something like this in your flow might give you what you are looking for:
logger = get_run_logger()
futures = f.map([1, 2, 3])
for future in futures:
    a, b = future.result()
    <http://logger.info|logger.info>(f"a is {a}, b is {b}")
👍 1
k

Kyle McEntush

12/24/2022, 7:45 PM
thanks, makes sense