https://prefect.io logo
Title
k

KEVIN K

05/22/2023, 11:16 PM
When I apply the task decorator to a method within a class, I'm getting the error that the method is expecting the "self" parameter, which it doesn't get. Is something like this supported, or should I keep all my flows and tasks in the main class?
n

Nate

05/22/2023, 11:27 PM
i would recommend having your class' methods invoke defined tasks instead of making the class methods themselves tasks would that pattern work in your case?
k

KEVIN K

05/25/2023, 4:04 AM
I'm not sure I understand what you're saying. The only way I know how to invoke a task is by calling the method. How would a class method invoke a task/method that is not defined in the class?
n

Nate

05/25/2023, 3:54 PM
@task
def some_work():
   pass

class Foo:
   def some_method(self, *args, **kwargs):
      return some_work(*args, **kwargs)

@flow
def some_flow():
   foo = Foo()
   foo.some_method()

if __name__ == "__main__":
   some_flow()
gives
09:53:10.221 | INFO    | prefect.engine - Created flow run 'porcelain-pillbug' for flow 'some-flow'
09:53:11.247 | INFO    | Flow run 'porcelain-pillbug' - Created task run 'some_work-0' for task 'some_work'
09:53:11.249 | INFO    | Flow run 'porcelain-pillbug' - Executing 'some_work-0' immediately...
09:53:11.915 | INFO    | Task run 'some_work-0' - Finished in state Completed()
09:53:12.043 | INFO    | Flow run 'porcelain-pillbug' - Finished in state Completed('All states completed.')
instead of doing something like
class Foo:
   @task
   def some_method(self, *args, **kwargs):
      pass