Python 气流:打开\u成功\u回调时,使用参数执行函数
我想使用从任务传递的一个参数执行函数 以下是我的状态参数函数:Python 气流:打开\u成功\u回调时,使用参数执行函数,python,scheduled-tasks,scheduler,airflow,apache-airflow,Python,Scheduled Tasks,Scheduler,Airflow,Apache Airflow,我想使用从任务传递的一个参数执行函数 以下是我的状态参数函数: def sns_notify(state): client = boto3.client('sns') if state == "failed": message = config.get('sns', 'message') + state else: message = config.get('sns', 'message') + state response = c
def sns_notify(state):
client = boto3.client('sns')
if state == "failed":
message = config.get('sns', 'message') + state
else:
message = config.get('sns', 'message') + state
response = client.publish(TargetArn=config.get('sns', 'target_arn'),
Message=message,
Subject=config.get('sns', 'subject'))
return response
以下是我的任务,状态为param:
t1 = DummyOperator(task_id='Dummy-1', trigger_rule=TriggerRule.ALL_SUCCESS,
on_success_callback=sns_notify("ok"), dag=dag)
t2 = DummyOperator(task_id='Dummy-2', trigger_rule=TriggerRule.ONE_FAILED,
on_success_callback=sns_notify("failed"), dag=dag)
当我运行dag时,该函数不会停止发送邮件(对于本例)每次通过气流加载dag时,它都会执行
sns\u notify(“ok”)
,因为您正在调用该函数。您需要只传递函数指针sns\u notify
,它将接收上下文。见文件:
trigger\u规则
与依赖项任务的执行方式相关,因此与\u success\u回调的无关
我不知道如何将变量传递给这个回调,但是-来这里寻找答案 Hoju指出了确切的错误
您可以使用函数式编程来帮助解决此问题
从functools导入部分
发送成功通知=部分(sns通知“确定”)
t1=DummyOperator(任务id='Dummy-1',触发器规则=TriggerRule.ALL\u成功,
on_success_callback=发送_success_通知,dag=dag)
发送失败通知=部分(sns通知,“失败”)
t2=DummyOperator(任务id='Dummy-2',触发器规则=TriggerRule.ONE失败,
on_success_callback=send_failure_通知,dag=dag)
响应
行应该使用else
进行换行。在这里加上4个空格,你就应该完成了。这是另外一件事,我只想用if,else初始化变量消息。你说不停止发送邮件是什么意思?在杀死执行者之前,您是否一直在接收电子邮件?我想,您需要将参数添加到上下文
字典中,该字典在调用成功时作为参数接收。