尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Airflow - setup and teardown

Airflow - setup and teardown
📅 发布时间:2026/6/22 4:31:22
Airflow - setup and teardown

 

"""
Example DAG demonstrating the usage of setup and teardown tasks.
"""from __future__ import annotationsimport pendulumfrom airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import DAG, TaskGroupwith DAG(dag_id='example_setup_teardown',schedule=None,start_date=pendulum.datetime(2021, 1, 1, tz='UTC'),catchup=False,tags=['example']
) as dag:root_setup = BashOperator(task_id='root_setup', bash_command="echo 'Hello from root_setup'").as_setup()root_normal = BashOperator(task_id='normal', bash_command="echo 'I am just a normal task'")root_teardown = BashOperator(task_id='root_teardown',bash_command="echo 'Goodbye from root_teardown'").as_teardown(setups=root_setup)root_setup >> root_normal >> root_teardownwith TaskGroup('section_1') as section_1:inner_setup = BashOperator(task_id='taskgroup_setup',bash_command="echo 'Hello from taskgroup_setup'").as_setup()inner_normal = BashOperator(task_id='normal', bash_command="echo 'I am just a normal task'")inner_teardown = BashOperator(task_id='taskgroup_teardown',bash_command="echo 'Hello from taskgroup_teardown'").as_teardown(setups=inner_setup)inner_setup >> inner_normal >> inner_teardownroot_normal >> section_1

 

Your code uses Airflow’s Setup & Teardown tasks, a feature introduced in Airflow 2.5+ to make resource-management patterns first-class citizens in DAGs.


✅ 1. What are Setup and Teardown tasks?

In Airflow, setup tasks run before dependent tasks to prepare resources.
Teardown tasks run after dependent tasks to clean up resources.

Examples:

  • Setup: create temp directory, open connection, prepare environment

  • Normal tasks: the main DAG work

  • Teardown: delete temp files, close connection, notify shutdown

Importantly:

✔ Teardown tasks always try to run

—even if upstream tasks fail—
because teardown is meant for cleanup.

✔ Setup and teardown create an implicit lifecycle

Airflow ensures the order and failure handling are correct even once the DAG gets complex.


🔧 2. What .as_setup() and .as_teardown() do

.as_setup()

Marks a task as a “setup task”.
Airflow registers it internally so teardown tasks know to wait for it and respect its lifecycle.

.as_teardown(setups=...)

Marks the task as a teardown task, and links it to the setup task(s).

It means:

  • Teardown will run after all tasks that depend on the setup

  • Teardown will run even if those tasks fail

  • Airflow guarantees teardown always runs exactly once per setup


🗂️ 3. Walkthrough of Your DAG

Let’s go piece by piece.


🌳 4. Root-level setup, normal, teardown

root_setup = BashOperator(...).as_setup()
root_normal = BashOperator(...)
root_teardown = BashOperator(...).as_teardown(setups=root_setup)root_setup >> root_normal >> root_teardown

✔ Execution flow

  1. root_setup runs first
    Output: “Hello from root_setup”

  2. root_normal runs
    Output: “I am just a normal task”

  3. root_teardown runs
    Output: “Goodbye from root_teardown”

✔ Behavior guarantees:

  • root_teardown always runs even if root_normal fails

  • It runs only after:

    • root_setup was done

    • All tasks that depend on root_setup (here: root_normal) have finished

So Airflow guarantees:

root_setup → root_normal → root_teardown

Regardless of upstream failures.


📦 5. TaskGroup setup, normal, teardown

Inside TaskGroup:

inner_setup = BashOperator(...).as_setup()
inner_normal = BashOperator(...)
inner_teardown = BashOperator(...).as_teardown(setups=inner_setup)inner_setup >> inner_normal >> inner_teardown

This is the same lifecycle but scoped inside the group:

✔ Life cycle inside TaskGroup

taskgroup_setup → normal → taskgroup_teardown

✔ Setup/Teardown in a group does NOT interact with the root-level ones

Each group manages its own setup/teardown boundary.


🔗 6. Cross-level dependency

root_normal >> section_1

This means:

  • All tasks in section_1 start after root_normal finishes.

So overall order becomes:

1

Important subtlety:

root_teardown does NOT wait for taskgroup_teardown

Because root_teardown is only linked to root_setup, not to inner setup.

This is intentional:
Setup/teardown relationships are scoped and isolated.


📌 7. Summary of Setup/Teardown Behavior in Your DAG

Root setup/teardown pair:

  • root_setup prepares something

  • root_teardown cleans up that same thing

  • It runs after everything depending on root_setup

TaskGroup setup/teardown pair:

  • taskgroup_setup prepares something for the group

  • taskgroup_teardown cleans up

  • It runs even if inner_normal fails

Independent life cycles:

  • Root setup/teardown does not control the TaskGroup

  • TaskGroup setup/teardown does not affect root teardown


🤓 8. Why use setup & teardown?

They make DAGs safer and more reliable:

  • Cleanup is guaranteed

  • Cleanup runs even if something crashes

  • DAGs become easier to reason about

  • Teardown tasks never accidentally run too early

  • Lifecycle boundaries are explicit and enforced

This is especially useful in:

    • Managing temporary clusters / VMs

    • Allocating / releasing cloud resources

    • Creating / deleting temp directories or batch environments

    • Opening / closing pools or exclusive resources

 

相关新闻

  • Django 实战总结:手把手教你搭建个人博客
  • 2025年飘雪机制造商权威推荐榜单:小型飘雪机/人工飘雪机/大型飘雪源头厂家精选 - 品牌推荐官
  • 2025年12月医学教育题库评测:昭昭题库的综合表现分析 - 品牌测评鉴赏家

最新新闻

  • PostgreSQL 12流复制在Ubuntu 20.04生产落地全指南
  • TRAE+GLM-5V-Turbo:多模态智能体运行时如何重塑AI编程工作流
  • Seedance 2.0 1080P技术解析:AI视频生成工作流质变突破
  • 如何用开源工具打造个人小说档案馆?终极数字内容保存方案详解
  • 2026年济南合同纠纷律师怎么挑?5个关键判断标准防踩雷 - 本地品牌推荐
  • Display Driver Uninstaller:彻底解决显卡驱动冲突的终极免费工具

日新闻

  • 2026速览惠州叛逆青少年学校前十大排名名单出炉 - 武汉中职最新信息发布
  • 2026上饶白蚁消杀哪家好?15年本土2大权威白蚁防治公司推荐(金盾虫控/青蚁卫士) - 我叫一
  • 天龙八部单机版终极数据管理工具:5个技巧快速掌握游戏数据编辑

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号