Search code examples
pythonpython-3.xprefect

Using prefect 2.0 flow on dataclass in python


I'm trying to wrap a class with a prefect flow, the flow currently works via class that looks something like this:

class Algorightm:

   @task
   def do_first(self, something):
      ....
  
   @task
   def do_second(self, something_else):
      ....

   @flow
   def start_flow(self):
      do_first(..)
      do_second(..)

Normally this works allright, and if I break this into external functions, the flow runs alright, meaning if I just use this outside of the Class, this works as needed:
This will work:

   @task
   def do_first(self, something):
      ....
  
   @task
   def do_second(self, something_else):
      ....

   @flow
   def start_flow(self):
      do_first(..)
      do_second(..)

But when using it inside a class, it gives me a recursion error. The question is, does prefect support using It's flow inside a Class? I could not find any info about it in the Docs


Solution

  • A Prefect flow must be a function rather than a class method. Otherwise, you won't be able to create a deployment for it. However, you can call your class within that function and Prefect even supports parameter validation with Pydantic (sharing this pydantic info in case you were using this class to get pydantic-model-style validation)