如何获取 运行 id 并将其放入 Airflow 中的变量中

How to get the run id and put it into a variable in Airflow

我想获取执行的DAG的run_id并将其作为DAG本身的变量。 有这个功能吗?

with DAG(
    dag_id='question',
    ...
) as dag:
    
    question = PythonOperator(
        task_id='question_python',
        python_callable=question_python,
        op_kwargs={run_id:#here },
    )

这里不用op_kwargsrun_id 是由 Airflow 提供的 macro 因此它在 python 可调用的上下文中可用:

def question_python(run_id, **context):
    # your function code

def question_python(**context):
    run_id = context[run_id]
    # your function code

如果您使用的是 Airflow < 2.0: 您还需要将 provide_context=True 添加到 PythonOperator