在Airflow中,任务操作的定义通常放在DAG文件中,而不是在其他文件中。如果你希望从其他文件加载任务操作,可以使用Python模块的方式进行加载。
下面是一个解决方法的示例代码:
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from some_module import my_task_function
dag = DAG('my_dag', schedule_interval='@once')
task = PythonOperator(
task_id='my_task',
python_callable=my_task_function,
dag=dag
)
some_module.py
)中定义任务操作的函数:def my_task_function():
# 任务操作的代码
print("Hello, Airflow!")
这样,当Airflow运行DAG时,它将从DAG文件中加载任务操作,并调用my_task_function
函数执行任务操作。
请注意,如果你想使用其他文件中的函数或类作为任务操作,你需要将这些文件放在可以被Airflow访问到的路径下,或者使用Python的sys.path
将其添加到搜索路径中。