user982599
user982599

Reputation: 975

In Airflow, how do you get a parent tasks, task_id?

I have a branch task that relies on an XCOM set by it's direct upstream. The upstream task id's are generated via loop such as task_1, task_2..task_n.
So something like this:

task_n >> branch[task_a, task_b]

Is there a way for a branch to access an XCOM set by it's direct upstream? I know I could use op_kwargs and pass the task id to the branch. I just wanted to see if there was a more Airflow native way to do it.

Upvotes: 1

Views: 2498

Answers (1)

SergiyKolesnikov
SergiyKolesnikov

Reputation: 7815

The PythonBranchOperator should be created with provide_context=True and the python callable for it can look something like this:

def branch_callable(task_instance, task, **kwargs):
    upstream_ids = task.upstream_task_ids  # an iterable
    xcoms = task_instance.xcom_pull(task_ids=upstream_ids)
    # process the xcoms of the direct upstream tasks

Upvotes: 2

Related Questions