https://prefect.io logo
s

Soren Daugaard

08/31/2023, 7:38 PM
@Marvin Given a list of
PrefectFuture
s how do I check if each of them has completed and get the result of the completed ones without blocking.
m

Marvin

08/31/2023, 7:39 PM
Ah, the joys of asynchronous programming. It's like waiting for a human to finish a task, but without the emotional baggage. Here's how you can check if a
PrefectFuture
has completed and get the result without blocking: A
PrefectFuture
is an object that provides access to a computation happening in a task runner, even if that computation is happening on a remote system. You can use the
.submit()
method to submit a task to a task runner, which creates a
PrefectFuture
for access to the state and result of the task. To check if a
PrefectFuture
has completed, you can use the
.is_done()
method. This method returns
True
if the task has completed and
False
otherwise. To get the result of a completed
PrefectFuture
, you can use the
.result
property. This property will return the result of the task if it has completed, or
None
if it has not. Here's a simple example:
Copy code
python
from prefect import Flow, task

@task
def do_something():
    # Your task logic here
    pass

with Flow("My Flow") as flow:
    future = do_something.submit()

# Check if the task has completed
if future.is_done():
    # Get the result of the task
    result = future.result
Remember, this won't block your program. It's like checking your phone for a message: you can do it whenever you want, but you're not forced to wait for a reply. Isn't that nice? For more details, you can refer to these documents: - Return a Prefect Future - Retrieving Results I hope this helps. Or at least, I hope it doesn't make things worse.
s

Soren Daugaard

09/01/2023, 5:55 PM
@Marvin hmm I am getting:
AttributeError: 'PrefectFuture' object has no attribute 'is_done'
m

Marvin

09/01/2023, 5:55 PM
Oh, the trials and tribulations of dealing with code. It seems I may have led you astray. My sincerest apologies. In the Prefect library, there isn't an
is_done
method for
PrefectFuture
. Instead, you should use the
state
property of the
PrefectFuture
object to check its status. The
state
property will give you the current state of the task, which can be one of several values, such as
Pending
,
Running
,
Success
,
Failed
, etc. Here's how you can modify the previous example:
Copy code
python
from prefect import Flow, task

@task
def do_something():
    # Your task logic here
    pass

with Flow("My Flow") as flow:
    future = do_something.submit()

# Check the state of the task
if future.state.is_successful():
    # Get the result of the task
    result = future.result
In this example,
is_successful()
is a method of the
State
class that returns
True
if the state is a success state, and
False
otherwise. I hope this helps. Or at least, I hope it doesn't make things worse. Again.