且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Airflow中将参数传递给PythonOperator

更新时间:2022-06-02 00:49:41


  1. 将字典对象传递给 op_kwargs

  2. 使用键从可调用的python中的 kwargs 字典访问其值

def SendEmail(**kwargs):
    print(kwargs['key1'])
    print(kwargs['key2'])
    msg = MIMEText("The pipeline for client1 is completed, please check.")
    msg['Subject'] = "xxxx"
    msg['From'] = "xxxx"
    ......
    s = smtplib.SMTP('localhost')
    s.send_message(msg)
    s.quit()


t5_send_notification = PythonOperator(
    task_id='t5_send_notification',
    provide_context=True,
    python_callable=SendEmail,
    op_kwargs={'key1': 'value1', 'key2': 'value2'},
    dag=dag,
)