Thread
#prefect-community
    j

    Jason Boorn

    10 months ago
    Quick question - if I have a set of tasks that don't depend on each other but that I want to run serially, what's the correct way to do that?
    Anna Geller

    Anna Geller

    10 months ago
    If you want to ensure that one task runs after another in a specific order, then you need to set upstream or downstream dependencies, as shown here:
    from prefect import task, Flow
    
    
    @task(log_stdout=True)
    def hello_world():
        print("hello world")
    
    
    @task(log_stdout=True)
    def hello_world_2():
        print("hello world")
    
    with Flow("basic-flow") as flow:
        t1 = hello_world()
        t2 = hello_world_2()
        t1.set_downstream(t2)
    
    if __name__ == "__main__":
        flow.visualize()
    but if the order is not important to you, you can just pass the list of tasks:
    from prefect import task, Flow
    
    
    @task(log_stdout=True)
    def hello_world():
        print("hello world")
    
    
    @task(log_stdout=True)
    def hello_world_2():
        print("hello world")
    
    flow = Flow("basic-flow", tasks=[hello_world, hello_world_2])
    
    if __name__ == "__main__":
        flow.visualize()
    this should still run one after the other when using the default LocalExecutor, but I think this way you can’t guarantee that hello_world runs before hello_world_2. Does it make sense?
    j

    Jason Boorn

    10 months ago
    Yup thats perfect - thanks
    Kevin Kho

    Kevin Kho

    10 months ago
    I just wanna add another syntax:
    with Flow("basic-flow") as flow:
        t1 = hello_world()
        t2 = hello_world_2(upstream_tasks=[t1])