<@ULVA73B9P> I have a task I'm using with `.submit...
# ask-marvin
t
@Marvin I have a task I'm using with
.submit()
in prefect 2. when I try and call
task_result.result()
I get
prefect.exceptions.UnfinishedRun: Run is in PENDING state, its result is not available.
. why is that?
m
The issue you're encountering with
task_result.result()
returning an
UnfinishedRun
exception is because the task is still in a
PENDING
state, meaning it hasn't completed yet. In Prefect 2.x, tasks submitted with
.submit()
return a future-like object. To ensure the task has finished before accessing its result, you should use the
.wait()
method on the future object, which blocks until the task completes. Here's a brief example of how you might structure this:
Copy code
python
from prefect import flow, task

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

@flow
def my_flow():
    future = my_task.submit()
    final_state = future.wait()  # Wait for the task to complete
    result = final_state.result()
    print(result)

my_flow()
This approach ensures that you handle the task state appropriately before attempting to access its result. For more details, you can refer to the Prefect documentation on task runners.