Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the context argument in BaseOperator.xcom_pull

Tags:

python

airflow

I was reading the API Docs, and was unclear what the context argument is for BaseOperator.xcom_pull.

I thought it would be dag.default_args, but I receive KeyError: 'ti'

I performed an xcom_push within a prior task following the push() example here.

like image 226
leon yin Avatar asked Oct 03 '16 15:10

leon yin


People also ask

What is context in Airflow?

Context Manager¶ Added in Airflow 1.8. DAGs can be used as context managers to automatically assign new operators to that DAG. with DAG('my_dag', start_date=datetime(2016, 1, 1)) as dag: op = DummyOperator('op') op. dag is dag # True.

What is Xcom_pull in Airflow?

XCom push/pull just adds/retrieves a row from the xcom table in the airflow DB based on DAG id, execution date, task id, and key.

What is the XCom in Airflow give an example?

XComs (short for “cross-communications”) are a mechanism that let Tasks talk to each other, as by default Tasks are entirely isolated and may be running on entirely different machines. An XCom is identified by a key (essentially its name), as well as the task_id and dag_id it came from.

What does Xcom_pull return?

The xcom_pull() method - It's used to pull a list of return values from one or multiple Airflow tasks. Note the plural of the first argument. Specify a list of task IDs from which you want to fetch values stored in XComs.


1 Answers

The context is a set of keyword arguments containing reference objects related to a task instance, such as dag, dag_run, run_id, execution_date, etc. (including the task instance ti itself).

The default context is generated when task instance runs, and is defined here.

In the example you mentioned, the way context is passed in isn't super obvious: if the provide_context arg is set to True, Airflow will pass the generated context to the python callable. You can then access the task instance's xcom_pull method by calling kwargs['ti'].xcom_pull().

So that's why for this to work, you will need to define **kwargs in your python callable's header, and set the operator's provide_context arg to True.

I am not sure how to directly get hold of the context reference though.

like image 159
joy gao Avatar answered Oct 15 '22 11:10

joy gao