Search code examples
pythondata-scienceworkflowprefect

Tasks sequence in Prefect Flow Python


I'm currently working with the Python framework - Prefect (prefect.io) I wrote the code below

from prefect import Flow, task
@task
def say_hello():
    print('Hello')

@task
def say_how_a_u():
    print('How are you?')

@task
def say_bye():
    print('Bye Bye')

with Flow('Test') as flow:
   say_hello()
   say_how_a_u()
   say_bye()

flow.run()

The fact is that all functions are called in parallel. How to make one function call after another and waited for the previous function? hello -> how_a_u -> bye

I work with triggers, but it fail


Solution

  • You can just specify the upstream dependencies during the Flow block. There is another syntax you can find here

    from prefect import Flow, task
    @task
    def say_hello():
        print('Hello')
    
    @task
    def say_how_a_u():
        print('How are you?')
    
    @task
    def say_bye():
        print('Bye Bye')
    
    with Flow('Test') as flow:
       a = say_hello()
       b = say_how_a_u(upstream_tasks=[a])
       c = say_bye(upstream_tasks=[b])
    
    flow.run()