Hello all. I am trying to write a simple task and get the return value from that task. I have been trying to follow https://docs.prefect.io/core/concepts/tasks.html#indexing as what is stated seems pretty straightforward. I have also been looking at the "Multiple Return Values" on that same page. Regardless of what I do, I can't get the expected values of 1 for the Indexing example, and the other values from the "Multiple Return Values" example. Rather, when I am printing the results, I get a <Task: xxxxxxx> type of string displayed. As stated above, I feel like this should be straightforward, but I must be missing something.
d
Dylan
03/29/2021, 2:27 PM
Hi @Jeff Williams!
Welcome to the Community 👋
Remember that different things happen when a Flow is defined and when a Flow is executed. Within a Flow’s context block, the output of any invoked Task is not the direct result of that Task when the file is first loaded. It only returns a value when the Flow is executed. So, Tasks don’t actually return the result you define directly. That result needs to be unpacked by another task
Dylan
03/29/2021, 2:27 PM
If you’d like to share your example code I can be more specific 😄
j
Jeff Williams
03/29/2021, 2:32 PM
HI Dylan,
I am literally copying the example in your docs, which I have repeated here and then doing the following
from prefect import Flow, task
@task
def fn():
return {'a': 1, 'b': 2}
with Flow('Indexing Flow') as flow:
x = fn()
y = x['a']
# I am adding the following line
print(f'a is{y}'
Which, from what I am reading in the docs, should be the value of 1.
d
Dylan
03/29/2021, 2:34 PM
Yes, but those don’t contain a print statement 😄
j
Jeff Williams
03/29/2021, 2:36 PM
I hit return before I was finished.... 🤣
d
Dylan
03/29/2021, 2:36 PM
Yup, that’s what I suspected!
Dylan
03/29/2021, 2:36 PM
y
in that example is a Task
Dylan
03/29/2021, 2:36 PM
You’ll need to pass it to another task to be unwrapped
Dylan
03/29/2021, 2:36 PM
Like this
Dylan
03/29/2021, 2:37 PM
Copy code
from prefect import Flow, task
@task
def fn():
return {'a': 1, 'b': 2}
@task
def print_result(input):
print(input)
with Flow('Indexing Flow') as flow:
x = fn()
y = x['a']
print_result(y)
j
Jeff Williams
03/29/2021, 2:41 PM
So two things:
1. The docs literally make the statement that "The result of that task (1) is stored as y", which says to me that y shouldn't be a task but a value.
2. How do you run a task that does "something" and then return its value. As in this case, what if I wanted to assign the value of "x['a']" to some value? Is this possible or am I missing a fundamental concept here? (very likely.... 😆 )
d
Dylan
03/29/2021, 2:46 PM
Remember that within a context block, we’re building a computational graph. Execution is deferred until runtime
j
Jeff Williams
03/29/2021, 2:46 PM
Similarly on the "Multiple Return Values" example. It seems like you should be able to get return values form a Task, but I can't get it figured out. I did have some success if I have a mapped input and the serialize the resulting task to get an output dictionary. But not all tasks have a need to be mapped. (If that makes any sense.)
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.