<@ULVA73B9P> using prefect 2 example of calling 3 ...
# ask-marvin
m
@Marvin using prefect 2 example of calling 3 deployments using run_deployment and waiting for all three to come back
m
In Prefect 2.x, running multiple deployments and waiting for all of them to complete can be achieved using the
run_deployment
function. Below is an example that demonstrates how to accomplish this: 1. Define a main flow that triggers three deployments. 2. Use
asyncio.gather
to wait for all deployments to complete. Here is a sample code snippet to demonstrate this:
Copy code
python
from prefect import flow
from prefect.deployments import run_deployment
import asyncio

@flow
async def main_flow():
    deployments = [
        {"name": "deployment_1", "parameters": {}},
        {"name": "deployment_2", "parameters": {}},
        {"name": "deployment_3", "parameters": {}},
    ]

    futures = [run_deployment(deployment["name"], parameters=deployment["parameters"]) for deployment in deployments]

    # Wait for all deployments to complete
    results = await asyncio.gather(*futures)
    for result in results:
        print(f"Deployment result: {result}")

if __name__ == "__main__":
    main_flow()
### Explanation 1. Defining the Main Flow: - An
async
flow named
main_flow
is defined. - A list of deployments is created with their names and parameters. 2. Running Deployments: -
run_deployment
is called for each deployment in the list, generating a list of futures. 3. Waiting for Results: -
asyncio.gather
is used to wait for all the futures to complete and collect their results. - The results are then printed. ### Sources - Prefect Deployment Documentation You may need to adjust the deployment names and parameters according to your specific use case.