如何将参数传递给Ai中的PythonOperator

2024-09-26 22:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用气流,有谁能教我如何将参数传递到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的callable(即SendEmail)中,理想情况下,我希望将完整日志和/或部分日志(基本上是从kwargs)附加到要发送的电子邮件中,猜测t5_send_notification是收集这些信息的地方。

非常感谢。


Tags: sendidtasknotificationmsgsendemailkwargsdag
2条回答

这应该有效:

t5_send_notification = PythonOperator(
    task_id='t5_send_notification',
    provide_context=True,
    python_callable=SendEmail,
    op_kwargs={my_param='value1'},
    dag=dag,
)

def SendEmail(my_param,**kwargs):
    print(my_param) #'value_1'
    msg = MIMEText("The pipeline for client1 is completed, please check.")
    msg['Subject'] = "xxxx"
    msg['From'] = "xxxx"
    ......
    s = smtplib.SMTP('localhost')
    s.send_me
  1. 将dict对象传递给
  2. 在python callable中,使用键从kwargsdict访问它们的值

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

相关问题 更多 >

    热门问题