airflowairflow-taskflow

Airflow @task decorator not having the correct order


I'm trying the new TaskFlow API

I have 4 @task then I would love to have at the end t1 >> t2 >> t3 >>> t4.

I endup doing :

t1 = task1()
t2 = task2(t1)
t3 = task3(t2)
t4 = task4(t2)

t2.set_downstream(t3)
t3.set_downstream(t4)

But the result is :

enter image description here

I wanted a straight line


Solution

  • you have to do it in the following way:

    from airflow.decorators import dag, task
    from pendulum import datetime
    
    @dag(start_date=datetime(2024, 12, 1), catchup=False, schedule=None)
    def temp():
        @task
        def t1():
            print(1)
        
        @task
        def t2(t1_op):
            print(2)
    
        @task
        def t3(t2_op):
            print(3)
    
        @task
        def t4(t3_op):
            print(4)
        
        t4(t3(t2(t1())))
    
    temp()