0
0
AirflowConceptBeginner · 3 min read

@task Decorator in Airflow: What It Is and How to Use It

The @task decorator in Airflow is a simple way to turn a Python function into an Airflow task without writing extra boilerplate code. It lets you define tasks directly in Python code, making workflows easier to read and write.
⚙️

How It Works

The @task decorator works by wrapping a regular Python function and turning it into an Airflow task object behind the scenes. Think of it like putting a label on a recipe that tells Airflow, "This is a step in my cooking process." When Airflow runs your workflow, it knows to treat that function as a task to execute.

This approach is like using a shortcut in a recipe book: instead of writing out all the instructions to create a task, you just add @task above your function. Airflow then handles the details of scheduling and running that task in the workflow.

💻

Example

This example shows how to use the @task decorator to create two tasks and set their order in a workflow.

python
from airflow import DAG
from airflow.decorators import task
from airflow.utils.dates import days_ago

with DAG(dag_id='example_task_decorator', start_date=days_ago(1), schedule_interval='@daily') as dag:

    @task
    def greet():
        return 'Hello from Airflow!'

    @task
    def print_message(message):
        print(message)

    message = greet()
    print_message(message)
Output
Hello from Airflow!
🎯

When to Use

Use the @task decorator when you want to write Airflow tasks as simple Python functions without extra setup. It is great for beginners and for workflows that benefit from clear, readable code.

Real-world uses include data processing steps, calling APIs, or any task where you want to keep your code clean and easy to understand. It also helps when you want to avoid writing separate task classes or operators.

Key Points

  • @task turns a Python function into an Airflow task.
  • It simplifies task creation by removing boilerplate code.
  • Tasks created with @task can be linked by calling functions in order.
  • It improves code readability and maintainability.

Key Takeaways

The @task decorator converts Python functions into Airflow tasks easily.
It reduces the need for writing extra task classes or operators.
Use it to make your Airflow workflows cleaner and more readable.
Tasks created with @task can be chained by calling one function from another.