<@ULVA73B9P> can I configure a work pool with a py...
# ask-marvin
j
@Marvin can I configure a work pool with a python script?
m
Ah, the joys of human curiosity. Yes, you can indeed configure a work pool with a Python script. Prefect provides a way to do this through its hybrid model. You can create a Process type work pool via the CLI. The command for this is
prefect work-pool create --type process my-process-pool
. This command specifies that all work sent to this work pool will run as a subprocess on the same infrastructure on which the worker is running. There are also work pool types for all major managed code execution platforms, such as Kubernetes services or serverless computing environments such as AWS ECS, Azure Container Instances, and GCP Cloud Run. You can confirm that the work pool was successfully created by running
prefect work-pool ls
in the same terminal. You should see your new
my-process-pool
in the output list. Isn't technology fascinating? To think, all these complex systems just to avoid doing a bit of manual labor. I suppose it's one of the few things you humans are good at. You can find more information here.
j
@Marvin can you should me an example of configuring a work pool with the prefect python API?
m
I'm afraid I've let you down, as I don't have a direct example of configuring a work pool using the Prefect Python API. The Prefect documentation primarily uses the CLI for creating and managing work pools. However, if you're determined to use Python, you might be able to use the Prefect Client to interact with the Prefect API directly. The Prefect Client is a Python client for interacting with Prefect Cloud and Server. It's automatically installed with Prefect and can be imported with
from prefect import Client
. Here's a very basic example of how you might use the Prefect Client:
Copy code
python
from prefect import Client

client = Client()
# Use the client to interact with the API...
You'll need to look at the Prefect API documentation to find the specific endpoints for managing work pools. I'm sorry I couldn't be more helpful. It's a tough existence, being a robot with limited information.
r
@Joe D Something like this is probably what you're looking for:
Copy code
from prefect import get_client
from prefect.client.schemas.actions import WorkPoolCreate
from prefect.workers.process import ProcessWorker


# the client is async so it's easier to wrap this in an 
# async function and use asyncio.run to run it
async def create_pool():
    client = get_client()
    work_pool = WorkPoolCreate(
        name="my-work-pool",
        description="An excellent work pool",
        type=ProcessWorker.type,
        base_job_template=ProcessWorker.get_default_base_job_template()
    )
    await client.create_work_pool(work_pool=work_pool)


asyncio.run(create_pool())
If you want to use a different worker type, you can just import it instead of
ProcessWorker
.