prefect.context
Async and thread safe models for passing runtime context data.
These contexts should never be directly mutated by the user.
For more user-accessible information about the current run, see prefect.runtime.
Functions
serialize_context
hydrated_context
get_run_context
- A
FlowRunContextorTaskRunContextdepending on the function type.
RuntimeError: If called outside of a flow or task run.
get_settings_context
prefect.context.use_profile for more details.
tags
use_profile
profile: The name of the profile to load or an instance of a Profile.override_environment_variable: If set, variables in the profile will take precedence over current environment variables. By default, environment variables will override profile settings.include_current_context: If set, the new settings will be constructed with the current settings context as a base. If not set, the use_base settings will be loaded from the environment and defaults.
root_settings_context
- Command line via ‘prefect —profile <name>’
- Environment variable via ‘PREFECT_PROFILE’
- Profiles file via the ‘active’ key
Classes
ContextModel
A base model for context data that forbids mutation and extra data while providing
a context manager
Methods:
get
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
serialize
SyncClientContext
A context for managing the sync Prefect client instances.
Clients were formerly tracked on the TaskRunContext and FlowRunContext, but
having two separate places and the addition of both sync and async clients
made it difficult to manage. This context is intended to be the single
source for sync clients.
The client creates a sync client, which can either be read directly from
the context object OR loaded with get_client, inject_client, or other
Prefect utilities.
with SyncClientContext.get_or_create() as ctx:
c1 = get_client(sync_client=True)
c2 = get_client(sync_client=True)
assert c1 is c2
assert c1 is ctx.client
Methods:
get
get_or_create
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
serialize
AsyncClientContext
A context for managing the async Prefect client instances.
Clients were formerly tracked on the TaskRunContext and FlowRunContext, but
having two separate places and the addition of both sync and async clients
made it difficult to manage. This context is intended to be the single
source for async clients.
The client creates an async client, which can either be read directly from
the context object OR loaded with get_client, inject_client, or other
Prefect utilities.
with AsyncClientContext.get_or_create() as ctx:
c1 = get_client(sync_client=False)
c2 = get_client(sync_client=False)
assert c1 is c2
assert c1 is ctx.client
Methods:
get
get_or_create
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
serialize
RunContext
The base context for a flow or task run. Data in this context will always be
available when get_run_context is called.
Attributes:
start_time: The time the run context was enteredclient: The Prefect client instance being used for API communication
get
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
serialize
serialize
EngineContext
The context for a flow run. Data in this context is only available from within a
flow run function.
Attributes:
flow: The flow instance associated with the runflow_run: The API metadata for the flow runtask_runner: The task runner instance being used for the flow runrun_results: A mapping of result ids to run states for this flow runlog_prints: Whether to log print statements from the flow runparameters: The parameters passed to the flow rundetached: Flag indicating if context has been serialized and sent to remote infrastructureresult_store: The result store used to persist resultspersist_result: Whether to persist the flow run resulttask_run_dynamic_keys: Counter for task calls allowing unique keysobserved_flow_pauses: Counter for flow pausesevents: Events worker to emit events
serialize
serialize
TaskRunContext
The context for a task run. Data in this context is only available from within a
task run function.
Attributes:
task: The task instance associated with the task runtask_run: The API metadata for this task run
serialize
serialize
AssetContext
The asset context for a materializing task run. Contains all asset-related information needed
for asset event emission and downstream asset dependency propagation.
Attributes:
direct_asset_dependencies: Assets that this task directly depends on (from task.asset_deps)downstream_assets: Assets that this task will create/materialize (from MaterializingTask.assets)upstream_assets: Assets from upstream task dependenciesmaterialized_by: Tool that materialized the assets (from MaterializingTask.materialized_by)task_run_id: ID of the associated task runmaterialization_metadata: Metadata for materialized assets
add_asset_metadata
asset_key: The asset keymetadata: Metadata dictionary to add
ValueError: If asset_key is not in downstream_assets
asset_as_related
asset_as_resource
emit_events
from_task_and_inputs
task: The task instancetask_run_id: The task run IDtask_inputs: The resolved task inputs (TaskRunResult objects)copy_to_child_ctx: Whether this context should be copied on a child AssetContext
- Configured AssetContext
get
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
related_materialized_by
serialize
serialize
update_tracked_assets
TagsContext
The context for prefect.tags management.
Attributes:
current_tags: A set of current tags in the context
get
get
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.
serialize
SettingsContext
The context for a Prefect settings.
This allows for safe concurrent access and modification of settings.
Attributes:
profile: The profile that is in use.settings: The complete settings model.
get
get
model_copy
include: Fields to include in new model.exclude: Fields to exclude from new model, as with values this takes precedence over include.update: Values to change/add in the new model. Note: the data is not validated before creating the new model - you should trust this data.deep: Set toTrueto make a deep copy of the model.
- A new model instance.