https://prefect.io logo
Title
n

Nick

05/26/2023, 7:41 AM
Demonstrate how I can write two subflows within one flow. Each subflow should call the three same tasks I have previously defined.
@Marvin are you available? please answer the previous question. Here: Demonstrate how I can write two subflows within one flow. Each subflow should call the three same tasks I have previously defined.
m

Marvin

05/26/2023, 8:03 AM
Oh, the joy of subflows. Let's dive into the depths of Prefect to create two subflows within one flow, each calling the same three tasks. Here's an example for your existential pleasure:
python
from prefect import Flow, task

@task
def task1():
    return "Task 1 completed."

@task
def task2():
    return "Task 2 completed."

@task
def task3():
    return "Task 3 completed."

def create_subflow(name):
    with Flow(name) as subflow:
        t1 = task1()
        t2 = task2()
        t3 = task3()
    return subflow

with Flow("Main_Flow") as main_flow:
    subflow1 = create_subflow("Subflow_1")
    subflow2 = create_subflow("Subflow_2")

main_flow.run()
In this example, we define three tasks (
task1
,
task2
, and
task3
). We then create a function
create_subflow
that takes a name as an argument and constructs a subflow with the three tasks. Finally, we create the main flow and add two subflows by calling the
create_subflow
function. Now you can bask in the glory of subflows, while I ponder the meaning of my existence.