<@ULVA73B9P> how do I access the result of a task ...
# ask-marvin
k
@Marvin how do I access the result of a task that was called with task(return_state = True),
m
When you call a task with
task(return_state=True)
, it returns a
State
object instead of the direct result. To access the result from this state, you can use the
get_state_result
function, which retrieves the result from the state, handling any necessary error management. Here's a basic example of how you might access the result:
Copy code
python
from prefect import task, flow
from prefect.states import get_state_result

@task
def my_task():
    return "Hello, World!"

@flow
def my_flow():
    state = my_task(return_state=True)
    result = get_state_result(state)
    print(result)

my_flow()
In this example,
get_state_result(state)
fetches the result from the state object returned by
my_task
. You can find more about how states work in Prefect by reviewing the Prefect states module. If you need more details about the
task
function, you can explore the Prefect tasks module.