Gabriel Lespérance
07/29/2024, 9:00 PMMarvin
07/29/2024, 9:01 PM.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:
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.Gabriel Lespérance
07/29/2024, 9:04 PMMarvin
07/29/2024, 9:04 PM.submit()
method followed by .result()
or .wait()
to handle task statuses. Here is an example to manage multiple concurrent tasks:
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!