将其他参数传递给on

编程入门 行业动态 更新时间:2024-10-28 04:29:50
本文介绍了将其他参数传递给on_failure_callback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将其他参数传递给我的on_failure_callback函数,但似乎只需要上下文。如何将其他参数传递给该函数...尤其是因为我想在一个单独的模块中定义该函数,以便可以在我的所有DAGS中使用它。

I'd like to pass other arguments to my on_failure_callback function but it only seems to want "context". How do I pass other arguments to that function...especially since I'd like to define that function in a separate module so it can be used in all my DAGS.

我当前的default_args如下:

My current default_args looks like this:

default_args = { 'owner': 'Me', 'depends_on_past': True, 'start_date': datetime(2016,01,01), 'email': ['me@me'], 'email_on_failure': False, 'email_on_retry': False, 'retries': 1, 'retry_delay': timedelta(minutes=1), 'on_failure_callback': notify_failure, 'max_active_runs': 1 }

如果我尝试类似这种方式,则会抱怨:

If I try something like this airflow complains:

default_args = { 'owner': 'Me', 'depends_on_past': True, 'start_date': datetime(2016,01,01), 'email': ['me@me'], 'email_on_failure': False, 'email_on_retry': False, 'retries': 1, 'retry_delay': timedelta(minutes=1), 'on_failure_callback': notify_failure(context,arg1,arg2), 'max_active_runs': 1 }

所以不确定如何传递arg1和arg我想在一个单独的模块中定义的我的notify_failure函数2可以简单地导入我的DAG中

so not sure how to pass arg1 and arg2 to my notify_failure fuction that I would like to define in a separate module that I can simply import into my DAG

推荐答案

您可以在DAG级别定义args,然后可以使用partials包。即:

Assuming the args are something you can define at the DAG level, then you can use the partials package. ie:

from functools import partial def generic_failure(arg1, arg2, context): # do whatever default_args = { 'owner': 'Me', 'depends_on_past': True, 'start_date': datetime(2016,01,01), 'email': ['me@me'], 'email_on_failure': False, 'email_on_retry': False, 'retries': 1, 'retry_delay': timedelta(minutes=1), 'on_failure_callback': partial(generic_failure, arg1, arg2), 'max_active_runs': 1 }

调用 partial(generic_failure,arg1,arg2)将返回一个函数,但预计 generic_failure 中仍有许多参数,在上例中,该参数只是单个参数 context

Calling partial(generic_failure, arg1, arg2) will return a function expecting however many arguments are remaining in generic_failure, which in the above example is just the single param context

更多推荐

将其他参数传递给on

本文发布于:2023-11-23 10:42:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!