Greetings to all, I am currently attempting to dev...
# prefect-community
e
Greetings to all, I am currently attempting to develop a new architecture utilizing Prefect 2 and I would like to manage classes by having them inherit from the Task class of Prefect. Would you be able to inform me of where I may find an example of this? I have been reading the post (https://discourse.prefect.io/t/can-i-define-my-tasks-as-classes-rather-than-functions/54) however, it is not very clear to me on how to manage runners when using Tasks as a superclass or in general, how to pass parameters and manage each function within the class as a subflow or task within a function.
n
Hi @Elliot Løvås - to take a quote from that post
While you can build your task logic within a class, we recommend defining tasks as functions. Since the flow’s computational graph is discovered at runtime, there is no need for a rigid class-based task definition.
Classes are stateful, while tasks are executed in a stateless manner to support concurrency and distributed parallelism. Additionally, splitting the orchestration framework into two APIs confuses many users.
If you need to define some logic in classes, you can still either:
• call them in your functional tasks,
• initialize and invoke class methods directly in your flow.
We don't have examples of this because its not a pattern we generally recommend. If you'd like to describe your use case, I'd be happy to give my 2 cents on how you might achieve it with prefect using a functional approach
e
why is it suggested a functional approach?
n
We generally prefer the pattern of wrapping important code that you want to orchestrate in task definitions, instead of trying to maintain some 3rd party sense of state in a class that inherits from
Task
. The way I think about it is, I want the prefect engine to manage the state of things for me, I don't want to assume that responsibility by potentially changing the nature of a task / how they communicate their state to the API. that said, there's nothing stopping you from doing so, in my head it just opens the door for unexpected behavior some other patterns I'd recommend if you just want to customize how certain tasks work • you can inject functionality into tasks without changing how the base prefect task works by using additional decorators (like this, or this) • you can define your own class independently of
Task
and use it within task decorated functions as desired • you can dynamically configure existing tasks by using
.with_options