liveBook · Manning

Context manager

As explained in Data Pipelines with Apache Airflow, Second Edition

A Python context manager is a class-based language feature that acquires resources in __enter__() and releases them in __exit__().

A Python context manager uses the with keyword to manage resources through __enter__() and __exit__(). In Airflow, it defines a DAG scope so tasks can reference the active DAG without receiving a dag argument. section 2.2

Defining an Airflow DAG

with DAG(dag_id="download_rocket_launches", start_date=..., schedule=None): download_launches = BashOperator(task_id="download_launches", ...) get_pictures = PythonOperator(task_id="get_pictures", ...) download_launches >> get_pictures >> notify section 2.2

The context-manager approach trades explicitness for less repetitive DAG wiring:

  • Reduces verbosity because tasks do not repeatedly receive the DAG object. section 2.2
  • Limits the context-manager scope to one DAG, which can restrict files defining multiple workflows. section 2.2
  • Provides an alternative to explicit DAG definitions and the Taskflow API. section 2.2
Listing 2.3 Instantiating a DAG object
with DAG(                                           #1
   dag_id=02_download_rocket_launches",             #2
   start_date=pendulum.today('UTC').add(days=-14),  #3
   schedule=None,
):

Inspect the compact DAG instantiation that establishes the context for its tasks. section 2.2

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Defines __enter__() and __exit__(), compares DAG-definition approaches, and stops short of implementing a custom context manager.
Examples
Uses a rocket-launch workflow with a DAG, BashOperator, PythonOperator, and download-to-processing task dependencies.
What it assumes
Assumes basic Python familiarity while defining the with keyword and resource-management methods from zero.
What you can do after
Apply it via Airflow to define a compact DAG and connect tasks without repeating the DAG argument.
How it connects
Connects context managers with Airflow DAGs, tasks, explicit DAG definitions, and the Taskflow API.

Also explained in

Compare how 4 books explain context manager →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.