本文介绍了将其他参数传递给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': ['[email protected]'],
  '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': ['[email protected]'],
  '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': ['[email protected]'],
  '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_failure_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:02