Hello all. I am trying to write a simple task and...
# ask-community
j
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
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
If you’d like to share your example code I can be more specific 😄
j
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
Yes, but those don’t contain a print statement 😄
j
I hit return before I was finished.... 🤣
d
Yup, that’s what I suspected!
y
in that example is a Task
You’ll need to pass it to another task to be unwrapped
Like this
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
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
Remember that within a context block, we’re building a computational graph. Execution is deferred until runtime
j
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.)
As long as you pass those return values to a task, everything will behave exactly like you expect 😄
Within a Flow context block, we’re not dealing directly with any data, we’re building a graph for later execution