【Airflow】TaskGroup使用实例

💡 A collection of closely related tasks on the same DAG that should be grouped together visually.

高度相关的任务成为一个任务组

文档简介

When set_downstream() or set_upstream() are called on the TaskGroup, it is applied across all tasks within the group if necessary.
设置上下游任务的时候可以设为任务组,有必要时会应用于组内所有的任务。

源码收获

参数

  • group_id:任务组名称,对应task_id
  • prefix_group_id:子任务id和子任务组id前是否会加上这个group_id,默认加上(True)
  • parent_group:当前任务组的上一级任务组。parent_group设置为None,则为根任务组。
  • dag:任务组所属的dag。
  • default_args
  • tooltip: TaskGroup节点在UI中显示的工具提示
  • ui_color: TaskGroup节点在UI中显示时的填充颜色
  • ui_fgcolor: TaskGroup节点在UI中显示时的标签颜色
  • add_suffix_on_collision:如果任务组名称已经存在,自动添加’ __1 '等后缀

使用实例

import datetime
from airflow import DAG
from airflow.decorators import task_group
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.operators.empty import EmptyOperator

with DAG(
    dag_id="test_task_group",
    tags=['test'],
    start_date=datetime.datetime(2024, 8, 1),
    schedule="@daily",
    default_args={"retries": 1},
):
    
    def print_log():
        print(f'task is running at {datetime.datetime.now()}.')

    @task_group(default_args={"retries": 3})
    def group1():
        """This docstring will become the tooltip for the TaskGroup."""
        task1 = PythonOperator(
            task_id="task1",
            python_callable=print_log
            )
        task2 = BashOperator(task_id="task2", bash_command="echo Hello World!", retries=2)
        print(task1.retries)  # 3
        print(task2.retries) 

    task3 = PythonOperator(
        task_id="task3",
        python_callable=print_log
        )

    group1() >> task3

任务组中的任务可以收起

总结

task group本质是将多个任务同时进行触发时,使用的。也就是一个节点需要有多个操作时,可以考虑使用task group。

参考链接

airflow.decorators.task_group — Airflow Documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值