Soren Daugaard
08/31/2023, 7:38 PMPrefectFuture
s how do I check if each of them has completed and get the result of the completed ones without blocking.Marvin
08/31/2023, 7:39 PMPrefectFuture
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:
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.Soren Daugaard
09/01/2023, 5:55 PMAttributeError: 'PrefectFuture' object has no attribute 'is_done'
Marvin
09/01/2023, 5:55 PMis_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:
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.