气流Python操作员传递参数

时间:2019-02-15 21:21:07

标签: python airflow

我正在尝试在气流DAG中编写Python运算符,并将某些参数传递给Python可调用对象。

我的代码如下所示。

def my_sleeping_function(threshold):
   print(threshold)

fmfdependency = PythonOperator(
   task_id='poke_check',
   python_callable=my_sleeping_function,
   provide_context=True,
   op_kwargs={'threshold': 100},
   dag=dag)

end = BatchEndOperator(
   queue=QUEUE,
   dag=dag)

start.set_downstream(fmfdependency)
fmfdependency.set_downstream(end)

但是我一直收到以下错误。

  

TypeError:my_sleeping_function()获得了意外的关键字参数'dag_run'

无法弄清原因。

2 个答案:

答案 0 :(得分:1)

在阈值参数之后,将** kwargs添加到操作员参数列表中

答案 1 :(得分:0)

这是您如何在Airflow中为Python运算符传递参数的方法。

from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

from time import sleep
from datetime import datetime

def my_func(*op_args):
        print(op_args)
        return op_args[0]

with DAG('python_dag', description='Python DAG', schedule_interval='*/5 * * * *', start_date=datetime(2018, 11, 1), catchup=False) as dag:
        dummy_task      = DummyOperator(task_id='dummy_task', retries=3)
        python_task     = PythonOperator(task_id='python_task', python_callable=my_func, op_args=['one', 'two', 'three'])

        dummy_task >> python_task