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
Copy code
@task
def f(val):
return val * 2, val * 4
then the call in my flow looks like
Copy code
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:
Copy code
# 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:
Copy code
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}")
Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.