gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:34:04 25 4
gpt4 key购买 nike

我刚开始使用 Airflow,谁能告诉我如何将参数传递给 PythonOperator,如下所示:

t5_send_notification = PythonOperator(
task_id='t5_send_notification',
provide_context=True,
python_callable=SendEmail,
op_kwargs=None,
#op_kwargs=(key1='value1', key2='value2'),
dag=dag,
)

def SendEmail(**kwargs):
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 的可调用函数,即 SendEmail,理想情况下我想附上完整的日志和/或部分日志(基本上来自 kwargs)到要发送的电子邮件,猜测 t5_send_notification 是收集这些信息的地方。

非常感谢。

最佳答案

  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,
    )

关于python - 如何在 Airflow 中将参数传递给 PythonOperator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54894418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com