airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Change set_current_context location

Open romsharon98 opened this issue 9 months ago • 0 comments

closes: https://github.com/apache/airflow/issues/24483

checked this changed works by this dag:


from airflow.operators.python import get_current_context, PythonOperator


def some_function_in_your_library():
    return 'rom'

def some_function_in_your_library2(name):
    print(name)


def config(task_id):
    context = get_current_context()
    _result = context['ti'].xcom_pull(task_ids=task_id)
    return _result

user_defined_macros = {"config": config}

from airflow.models.dag import DAG

with DAG(dag_id='my_dag', user_defined_macros=user_defined_macros) as dag:
    task1 = PythonOperator(python_callable=some_function_in_your_library, task_id='my_task')
    task2 = PythonOperator(python_callable=some_function_in_your_library2, task_id='my_task2', op_kwargs={"name": "{{config('my_task')}}"})
    task1 >> task2

task2 print rom

cc: @vanchaxy


^ Add meaningful description above Read the Pull Request Guidelines for more information. In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed. In case of a new dependency, check compliance with the ASF 3rd Party License Policy. In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

romsharon98 avatar May 15 '24 11:05 romsharon98