<@ULVA73B9P> We have several flows to run. Each fl...
# ask-marvin
n
@Marvin We have several flows to run. Each flow need to run several tasks before the flow's logic. how to do that in Prefect?
m
To orchestrate several tasks to run before your flow's main logic in Prefect, you can follow these steps: 1. Define your tasks: In Prefect, first you need to define individual tasks using Python functions decorated with
@task
. 2. Create a flow: You define a flow by using the
with Flow("flow_name") as flow:
syntax. 3. Add tasks to the flow: Within the flow context, you can add tasks and define their order by setting dependencies between them. You will do this by calling the tasks like normal Python functions. 4. Set the flow's logic: Once your preparatory tasks are in the flow, you can continue to define the main logic of your flow by adding more tasks and specifying the execution order using dependencies. 5. Execute the flow: You can run the flow manually, through external schedulers, or by deploying and scheduling it on Prefect Cloud or a local Prefect server. Here's an example of how this might look in code:
Copy code
python
from prefect import Flow, task

@task
def preparatory_task_1():
    # Code for the first task before the flow's main logic
    pass

@task
def preparatory_task_2():
    # Code for the second task before the flow's main logic
    pass

@task
def main_flow_logic():
    # Code for the main flow logic
    pass

with Flow("My Flow") as flow:
    task_1_output = preparatory_task_1()
    task_2_output = preparatory_task_2(upstream_tasks=[task_1_output])
    main_flow_logic(upstream_tasks=[task_2_output])

# Now, you can run the flow by invoking `flow.run()` or scheduling it as needed.
Keep in mind that tasks must be called from within a flow and not from other tasks. The order is determined by dependencies, and Prefect handles the task orchestration for you once your flow is running.
n
I want to run tasks before any flow. But without duplicates the tasks. In prefect 1 we simply created a class (BaseFlow) that every flow class inherit, and the BaseFlow defines those tasks for every flow that inherit it. How to do that in Prefect 2?