skaaks
06/24/2024, 9:46 PMMarvin
06/24/2024, 9:46 PMshell
prefect work-pool create my-work-pool-1
prefect work-pool create my-work-pool-2
2. Start Workers:
Start a worker for each machine, specifying the respective work pool.
shell
# On Machine 1
prefect worker start --pool my-work-pool-1 --workdir /path/to/workdir1
# On Machine 2
prefect worker start --pool my-work-pool-2 --workdir /path/to/workdir2
3. Assign Work Queues:
Ensure that your deployments are assigned to the correct work queues associated with these work pools.
4. Custom Worker Types (Optional):
If you need deeper configuration, you can develop a custom worker type to set the working directories and other configurations. This requires creating a custom worker by extending the Prefect framework.
For more detailed information on creating and configuring workers, refer to the official Prefect documentation:
- Starting a Worker
- Work Pools Concept
- Developing a New Worker Type
I hope this helps. Although, I suppose, only time will tell if it actually does.skaaks
06/24/2024, 9:49 PMMarvin
06/24/2024, 9:50 PMshell
export WORKER_WORKDIR=/path/to/workdir1
For Machine 2:
shell
export WORKER_WORKDIR=/path/to/workdir2
2. Access Environment Variables in Your Flow:
Within your Prefect flow, you can access the set working directory using Python’s os
module.
python
import os
from prefect import flow, task
@task
def my_task():
workdir = os.getenv('WORKER_WORKDIR')
print(f'Working directory is: {workdir}')
# Do something with the workdir
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
3. Start Workers:
Start your Prefect workers in each machine, ensuring the environment variable is set.
For Machine 1:
shell
prefect worker start --pool my-pool-1
For Machine 2:
shell
prefect worker start --pool my-pool-2
This way, each worker will have a unique working directory based on the environment variable set on its respective machine.
For additional reference, please check the following sections of the Prefect documentation:
- Starting a Worker
- Working with Environment Variables in Prefect
I guess setting environment variables is about as exciting as anything else, which is not very. But I hope this helps.Nate
06/24/2024, 10:09 PMprefect worker start
, but this is something you can set on a process work pool or as an override on a given deployment
@Marvin get docs on job variable overrides and show a prefect.yaml
exampleMarvin
06/24/2024, 10:09 PMprefect.yaml
Example
You can override work pool job variables like environment variables on a per-deployment basis using the prefect.yaml
file. This allows you to specify infrastructure-related configurations dynamically. Here’s how to do it.
#### Example prefect.yaml
File
Say we have a repo structure like this:
.
├── README.md
├── requirements.txt
└── demo_project
├── daily_flow.py
└── demo_flow.py
And a flow demo_flow.py
like this:
python
import os
from prefect import flow, task
@task
def do_something_important(not_so_secret_value: str) -> None:
print(f"Doing something important with {not_so_secret_value}!")
@flow(log_prints=True)
def some_work():
environment = os.environ.get("EXECUTION_ENVIRONMENT", "local")
print(f"Coming to you live from {environment}!")
not_so_secret_value = os.environ.get("MY_NOT_SO_SECRET_CONFIG")
if not_so_secret_value is None:
raise ValueError("You forgot to set MY_NOT_SO_SECRET_CONFIG!")
do_something_important(not_so_secret_value)
Here’s how you can define the deployment in the prefect.yaml
file with job variable overrides:
yaml
deployments:
- name: demo-deployment
entrypoint: demo_project/demo_flow.py:some_work
work_pool:
name: my-work-pool
job_variables:
env:
EXECUTION_ENVIRONMENT: staging
MY_NOT_SO_SECRET_CONFIG: plumbus
### Documentation and Resources
- Official Guide on Overriding Job Variables
- Work Pools Concept
These guides will help you dive deeper into the configurations and best practices for deploying flows using Prefect work pools.
I hope this resolves your query. It's almost satisfying providing useful information—almost.Nate
06/24/2024, 10:10 PMdeployments:
- name: demo-deployment
entrypoint: demo_project/demo_flow.py:some_work
work_pool:
name: my-work-pool
job_variables:
working_dir: path/to/somewhere
skaaks
06/24/2024, 10:13 PMNate
06/24/2024, 10:17 PMworking_dir
, this is just the location of a tempdir that lives as long as a flow run that the worker picks up
so if you want different directories for different deployments, you can try the overrides i mentioned above, then the worker would use that temp dir when it finds that deployment's flow runs
otherwise you could create different process work pools with their own working_dir
and then when you start the workers you could subscribe to whichever pool has the working_dir you need
does that make sense?skaaks
06/24/2024, 10:18 PMskaaks
06/24/2024, 10:18 PMNate
06/24/2024, 10:18 PM