A DAG file calls an API and reads a Variable at import time — why does every DAG slow down?
After this file was added, every DAG in the deployment shows up late, the UI reports import timeouts for this file now and then, and the metadata service behind meta.internal sees a request every half minute even when no run is active. The DAG itself runs once an hour.
import pendulum
import requests
from airflow.sdk import DAG, Variable, task
CONFIG = requests.get("https://meta.internal/tables", timeout=60).json()
TARGET = Variable.get("warehouse_schema")
with DAG(
dag_id="sync_tables",
start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
schedule="@hourly",
catchup=False,
):
@task
def sync():
copy_tables(CONFIG["tables"], TARGET)
sync()Find and fix the bug.
The DAG processor re-imports every DAG file in a loop, so top-level code runs on each parse, not per run: the HTTP call and Variable.get fire every cycle, slow parsing of all files, risk dagbag_import_timeout and hammer the source. Move them into the task.
- Assuming module-level code runs once per DAG run rather than on every parse of the file
- Thinking a slow DAG file only hurts its own DAG, when it delays parsing for every file
- Keeping
Variable.getat module level instead of reading it in the task or a template
- Which settings control how often DAG files are re-parsed and how long one import may take?
- How would you pass the Variable through a Jinja template such as
var.valueinstead?
The DAG processor is a separate component that re-imports every DAG file in a loop (every min_file_process_interval, 30 seconds by default). Everything at module level runs on each import: an HTTP request with a 60-second timeout delays parsing of the other files too, can run into dagbag_import_timeout, and the service gets a request every half minute. Move the work into the task and read the Variable at run time:
import pendulum
import requests
from airflow.sdk import DAG, Variable, task
with DAG(
dag_id="sync_tables",
start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
schedule="@hourly",
catchup=False,
):
@task
def sync():
config = requests.get("https://meta.internal/tables", timeout=60).json()
target = Variable.get("warehouse_schema")
copy_tables(config["tables"], target)
sync()Now the request and the Variable read happen once an hour, when the task actually runs. For templated operators {{ var.value.warehouse_schema }} does the same: the template renders at run time, not at parse time.