airflow.models.xcom

Module Contents

Classes

BaseXCom

Base class for XCom objects.

Functions

resolve_xcom_backend()

Resolve custom XCom class.

Attributes

log

XCom

airflow.models.xcom.log[source]
class airflow.models.xcom.BaseXCom(context=None)[source]

Bases: airflow.models.base.TaskInstanceDependencies, airflow.utils.log.logging_mixin.LoggingMixin

Base class for XCom objects.

__tablename__ = 'xcom'[source]
dag_run_id[source]
task_id[source]
map_index[source]
key[source]
dag_id[source]
run_id[source]
value[source]
timestamp[source]
__table_args__ = ()[source]
dag_run[source]
logical_date[source]
init_on_load()[source]

Execute after the instance has been loaded from the DB or otherwise reconstituted; called by the ORM.

i.e automatically deserialize Xcom value when loading from DB.

__repr__()[source]

Return repr(self).

classmethod set(key, value, *, dag_id, task_id, run_id, map_index=-1, session=NEW_SESSION)[source]

Store an XCom value.

Parameters
  • key (str) – Key to store the XCom.

  • value (Any) – XCom value to store.

  • dag_id (str) – DAG ID.

  • task_id (str) – Task ID.

  • run_id (str) – DAG run ID for the task.

  • map_index (int) – Optional map index to assign XCom for a mapped task. The default is -1 (set for a non-mapped task).

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

static get_value(*, ti_key, key=None, session=NEW_SESSION)[source]

Retrieve an XCom value for a task instance.

This method returns “full” XCom values (i.e. uses deserialize_value from the XCom backend). Use get_many() if you want the “shortened” value via orm_deserialize_value.

If there are no results, None is returned. If multiple XCom entries match the criteria, an arbitrary one is returned.

Parameters
  • ti_key (airflow.models.taskinstancekey.TaskInstanceKey) – The TaskInstanceKey to look up the XCom for.

  • key (str | None) – A key for the XCom. If provided, only XCom with matching keys will be returned. Pass None (default) to remove the filter.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

static get_one(*, key=None, dag_id=None, task_id=None, run_id, map_index=None, session=NEW_SESSION, include_prior_dates=False)[source]

Retrieve an XCom value, optionally meeting certain criteria.

This method returns “full” XCom values (i.e. uses deserialize_value from the XCom backend). Use get_many() if you want the “shortened” value via orm_deserialize_value.

If there are no results, None is returned. If multiple XCom entries match the criteria, an arbitrary one is returned.

See also

get_value() is a convenience function if you already have a structured TaskInstance or TaskInstanceKey object available.

Parameters
  • run_id (str) – DAG run ID for the task.

  • dag_id (str | None) – Only pull XCom from this DAG. Pass None (default) to remove the filter.

  • task_id (str | None) – Only XCom from task with matching ID will be pulled. Pass None (default) to remove the filter.

  • map_index (int | None) – Only XCom from task with matching ID will be pulled. Pass None (default) to remove the filter.

  • key (str | None) – A key for the XCom. If provided, only XCom with matching keys will be returned. Pass None (default) to remove the filter.

  • include_prior_dates (bool) – If False (default), only XCom from the specified DAG run is returned. If True, the latest matching XCom is returned regardless of the run it belongs to.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

static get_many(*, run_id, key=None, task_ids=None, dag_ids=None, map_indexes=None, include_prior_dates=False, limit=None, session=NEW_SESSION)[source]

Composes a query to get one or more XCom entries.

This function returns an SQLAlchemy query of full XCom objects. If you just want one stored value, use get_one() instead.

Parameters
  • run_id (str) – DAG run ID for the task.

  • key (str | None) – A key for the XComs. If provided, only XComs with matching keys will be returned. Pass None (default) to remove the filter.

  • task_ids (str | collections.abc.Iterable[str] | None) – Only XComs from task with matching IDs will be pulled. Pass None (default) to remove the filter.

  • dag_ids (str | collections.abc.Iterable[str] | None) – Only pulls XComs from specified DAGs. Pass None (default) to remove the filter.

  • map_indexes (int | collections.abc.Iterable[int] | None) – Only XComs from matching map indexes will be pulled. Pass None (default) to remove the filter.

  • include_prior_dates (bool) – If False (default), only XComs from the specified DAG run are returned. If True, all matching XComs are returned regardless of the run it belongs to.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

  • limit (int | None) – Limiting returning XComs

classmethod delete(xcoms, session)[source]

Delete one or multiple XCom entries.

static purge(xcom, session)[source]

Purge an XCom entry from underlying storage implementations.

static clear(*, dag_id, task_id, run_id, map_index=None, session=NEW_SESSION)[source]

Clear all XCom data from the database for the given task instance.

Parameters
  • dag_id (str) – ID of DAG to clear the XCom for.

  • task_id (str) – ID of task to clear the XCom for.

  • run_id (str) – ID of DAG run to clear the XCom for.

  • map_index (int | None) – If given, only clear XCom from this particular mapped task. The default None clears all XComs from the task.

  • session (sqlalchemy.orm.Session) – Database session. If not given, a new session will be created for this function.

static serialize_value(value, *, key=None, task_id=None, dag_id=None, run_id=None, map_index=None)[source]

Serialize XCom value to JSON str.

static deserialize_value(result)[source]

Deserialize XCom value from str or pickle object.

orm_deserialize_value()[source]

Deserialize method which is used to reconstruct ORM XCom object.

This method should be overridden in custom XCom backends to avoid unnecessary request or other resource consuming operations when creating XCom orm model. This is used when viewing XCom listing in the webserver, for example.

airflow.models.xcom.resolve_xcom_backend()[source]

Resolve custom XCom class.

Confirm that custom XCom class extends the BaseXCom. Compare the function signature of the custom XCom serialize_value to the base XCom serialize_value.

airflow.models.xcom.XCom[source]

Was this entry helpful?