<@ULVA73B9P> why would someone use a subflow rathe...
# marvin-in-the-wild
m
@Marvin why would someone use a subflow rather than a task?
m
Subflows in Prefect should generally be used as opposed to tasks when you are dealing with a group of tasks that together perform a larger, cohesive piece of work within a workflow. Tasks are recommended for discrete, specific pieces of work like calling an API, performing a database operation, or analyzing/transforming a data point. Subflows, on the other hand, can encapsulate a series of steps that represent a complex operation and might need to be reused as a single unit within different workflows. You can call tasks or subflows to do work within your workflow and even pass results from other tasks to your subflow. When deciding whether to use a subflow or a task, consider the complexity and reusability of the operation you are trying to encapsulate. For more detailed guidance and examples, please refer to the Prefect documentation.
n
historically, this is pretty accurate > Subflows in Prefect should generally be used as opposed to tasks when you are dealing with a group of tasks that together perform a larger, cohesive piece of work within a workflow. we have recently gotten more flexible though, in that you're free to call tasks inside of tasks (previously you weren't so tasks were "bedrock" in the sense that you couldnt call more tasks inside of them ) so now you can do stuff like
Copy code
In [1]: from prefect import flow, task

In [2]: @task
   ...: def foo():
   ...:     @task
   ...:     def bar():
   ...:         pass
   ...:     bar()

In [3]:

In [3]: @flow
   ...: def baz():
   ...:     foo()
   ...:

In [4]: baz()

17:30:18.657 | INFO    | prefect.engine - Created flow run 'gentle-crocodile' for flow 'baz'
17:30:19.232 | INFO    | Flow run 'gentle-crocodile' - Created task run 'foo-0' for task 'foo'
17:30:19.235 | INFO    | Flow run 'gentle-crocodile' - Executing 'foo-0' immediately...
17:30:19.605 | INFO    | Task run 'foo-0' - Created task run 'bar-0' for task 'bar'
17:30:19.607 | INFO    | Task run 'foo-0' - Executing 'bar-0' immediately...
17:30:20.083 | INFO    | Task run 'bar-0' - Finished in state Completed()
17:30:20.231 | INFO    | Task run 'foo-0' - Finished in state Completed()
17:30:20.438 | INFO    | Flow run 'gentle-crocodile' - Finished in state Completed('All states completed.'
👀 1
m
Thanks Nate, with that in mind is there any advantage to using a subflow over nested tasks?