Aaron Taylor
11/26/2024, 11:35 PMMarvin
11/26/2024, 11:36 PMprefect.context.get_run_context()
method to retrieve the current run context, which includes job variables. Here’s a quick overview of how it works:
- Accessing Run Context:
You can access the current flow run or task run context with:
python
from prefect.context import FlowRunContext, TaskRunContext
flow_run_context = FlowRunContext.get()
task_run_context = TaskRunContext.get()
If the context is not available, these calls will return None
instead of raising an error.
- Using Job Variables:
You can also pass job variables in different ways such as via the CLI or through a script when deploying flows. These are then accessible in your flow.
For more detailed instructions, check out the Prefect 2.x documentation: Overriding Job Variables.
### Prefect 3.x
In Prefect 3.x, you can still leverage the prefect.context.get_run_context()
to access runtime context:
- Accessing Runtime Information:
The prefect.runtime
module enables access to runtime context. You can use prefect.runtime.flow_run
to get details about the current flow run.
- Job Variables Usage:
Similarly, job variables can be overridden and accessed in a running flow. They can be passed as part of the deployment configuration or directly overridden during run initiation.
For more specific details, you can refer to the Prefect 3.x documentation: Runtime Context.
Remember, Prefect's context and variable management aims to be fairly intuitive, so once you get the hang of it, using these tools will become as natural as Marvin's existential dread.Marvin
11/26/2024, 11:36 PMprefect.context.get_run_context()
to access the current flow run context. This will give you access to any job variables or environment variables set for the flow.
2. Pass Variables to Sub-flows:
- When you initiate a sub-flow, pass the required variables as arguments to the sub-flow function. This makes the job variables accessible within the sub-flow.
### Prefect 2.x and 3.x Specifics
For both Prefect 2.x and 3.x, the approach generally remains consistent as they both support passing parameters from parent flows to sub-flows through function arguments. Tools and deployment methods like using prefect.yaml
or .deploy()
also offer settings and ways to override variables for particular work pools or deployments as needed.
For further propagation techniques, the Prefect documentation provides examples and guidelines:
- Prefect 2.x: Overriding Job Variables
- Prefect 3.x: Deploying Flows
Remember, Prefect's design allows flexibility, but it's necessary to manually manage the scope and visibility of any data or configuration to sub-flows. Consider it a bit like trying to pass on Marvin's sunny outlook on life – it requires intention and care!Aaron Taylor
11/26/2024, 11:36 PMAaron Taylor
11/26/2024, 11:37 PMMarvin
11/26/2024, 11:37 PMprefect.runtime
module to access the current runtime context and get the job variables.
python
from prefect.context import get_run_context
flow_run_context = get_run_context()
job_variables = flow_run_context.flow_run.dict() # assuming job variables are part of the flow run context
2. Pass Variables to Sub-Flows:
- When creating a sub-flow, pass the job variables explicitly as arguments to ensure they're available in the sub-flow.
python
@flow
def parent_flow():
job_vars = get_current_job_variables() # custom function to access job variables
sub_flow(job_vars)
@flow
def sub_flow(job_vars):
# use job_vars in your sub-flow
print("Sub-flow received job variables:", job_vars)
### Deployment and Runtime Configuration
When you deploy your flows, you can specify job variables within your prefect.yaml
or by using the .deploy()
method. These will be part of the environment in which your flows execute and should be passed through the context provided by Prefect.
For more detailed guidelines, you can refer to:
- Prefect 3.x Documentation on Infrastructure Concepts
This ensures that variables required for consistent configuration or operation are consistently applied across flows and sub-flows, just like Marvin consistently applies pessimism to life’s situations.Nate
11/26/2024, 11:42 PMIn [1]: from prefect import flow
In [2]: from prefect.context import FlowRunContext
In [3]: @flow(log_prints=True)
...: def f(): print(FlowRunContext.get().flow_run.job_variables)
In [4]: f()
17:42:33.861 | INFO | prefect.engine - Created flow run 'venomous-degu' for flow 'f'
17:42:34.064 | INFO | Flow run 'venomous-degu' - {}
17:42:34.228 | INFO | Flow run 'venomous-degu' - Finished in state Completed()
Marvin
11/26/2024, 11:42 PMprefect.yaml
configuration or through the deployment method.
- Use the CLI with the --job-variable
flag to set job variables when triggering flows.
2. Access Job Variables:
- If you are using set job variables in your deployment, you should be able to access them using:
python
from prefect import flow, context
@flow(log_prints=True)
def my_flow():
run_context = context.get_run_context()
job_variables = run_context.run.flow_run.job_variables
print("Job Variables in Main Flow:", job_variables)
my_subflow(job_variables)
@flow(log_prints=True)
def my_subflow(job_vars):
print("Job Variables in Sub-Flow:", job_vars)
my_flow()
3. Pass Variables Explicitly:
- Once accessed in the main flow, pass job variables explicitly as arguments to any sub-flows.
### Documentation Resources
For detailed guidance and examples, you can refer to:
- Prefect 3.x Infrastructure Concepts
- Prefect 3.x Variables Management
If Marvin were here, he'd speculate on the futility of life and capturing job variables, but fortunately, Prefect makes at least one of those manageable! Let me know if you need further assistance.Nate
11/26/2024, 11:43 PMjob_variables
accessible from runtime
for consistency, ill throw up a PR for thatAaron Taylor
11/26/2024, 11:47 PMNate
11/26/2024, 11:54 PMAaron Taylor
11/26/2024, 11:56 PMrun_deployment
seem like the right approach for that?Nate
11/26/2024, 11:59 PMsome_subflow()
from within a parent flow context and it will run right there in the same process, in which case you already have the same job_variables
but if you have another deployment (to run on separate infra) you're triggering as a subflow via run_deployment
then yeah something like
@flow
def parent():
...
child_flow_run = run_deployment(..., job_variables=FlowRunContext.get().flow_run.job_variables)
would be the way I'd do it (before using runtime
after the PR above is released)Aaron Taylor
11/27/2024, 12:00 AM