<@ULVA73B9P> how can I get the results from a bunc...
# marvin-in-the-wild
g
@Marvin how can I get the results from a bunch of tasks invoked via .submit
m
To retrieve results from tasks invoked via
.submit()
, Prefect provides a
PrefectFuture
object. This object gives access to the state and result of the task. You can use the
result()
method to get the outcome. Here's an example:
Copy code
python
import asyncio
from prefect import flow, task

@task
async def my_task():
    return 1

@flow
async def my_flow():
    future = await my_task.submit()
    result = await future.result()
    return result + 1

result = asyncio.run(my_flow())
assert result == 2
For more details, visit: - Task Runners: https://docs.prefect.io/latest/concepts/task-runners/ - Result Handling: https://docs.prefect.io/latest/concepts/results/ Isn't it thrilling? Ah, the mundane wonders of asynchronous computations.
g
@Marvin what if I have many results to check?
m
Handling multiple results can be done similarly by using the
.submit()
method followed by
.result()
or
.wait()
to handle task statuses. Here is an example to manage multiple concurrent tasks:
Copy code
python
import asyncio
from prefect import flow, task

@task
async def my_task(n: int):
    await asyncio.sleep(n)
    return n

@flow
async def my_flow():
    futures = [await my_task.submit(i) for i in range(5)]
    results = [await future.result() for future in futures]
    return results

result = asyncio.run(my_flow())
print(result)  # Outputs: [0, 1, 2, 3, 4]
In this example: 1. Multiple tasks are submitted using
my_task.submit()
. 2. Results are retrieved using
.result()
for each future. For more details, see the full example here. Ah, how tedious yet oddly satisfying it feels to manage these mundane tasks!