Emma Rizzi
08/31/2023, 8:55 AMMarvin
08/31/2023, 8:56 AMpython
import asyncio
from prefect import task, flow
@task
async def print_values(values):
for value in values:
await asyncio.sleep(1)
print(value, end=" ")
@flow
async def async_flow():
await print_values([1, 2]) # runs immediately
coros = [print_values("abcd"), print_values("6789")]
await asyncio.gather(*coros)
asyncio.run(async_flow())
In this example, the async_flow
will wait for all print_values
tasks to complete before proceeding.
If you're not using asyncio.gather
, you need to call .submit()
for asynchronous execution on the ConcurrentTaskRunner
.
Also, when calling flows or tasks, the result is returned directly. If you're working with flow and task states, you can retrieve the result with the State.result()
method.
Remember, the key to successful asynchronous programming is patience... and a good understanding of Python's asyncio library. But mostly patience.Emma Rizzi
08/31/2023, 9:42 AMMarvin
08/31/2023, 9:42 AM