Hello guys. I need to make a class method callabl...
# prefect-community
n
Hello guys. I need to make a class method callable into a task object. Was trying to use the utility
as_task
but, even with global scoped functions isn’t working as expected. Current code while testing:
Copy code
import prefect
from prefect import task, Flow
from prefect.utilities.tasks import as_task


def hello_world():
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>("Hello, World!")


flow = Flow("hello-flow", tasks=[as_task(hello_world)])


if __name__ == "__main__":
    flow.run()
Does anyone has a suggestion? Thank you.
z
Hi!
as_task
is intended to convert some python builtins to tasks but does not include converting a function to a task. For that, you’ll want the
FunctionTask
which is what the
@task
decorator uses https://docs.prefect.io/api/latest/tasks/function.html. You can also use the
task
decorator as a function instead e.g.
Copy code
In [12]: import prefect
    ...: from prefect import task, Flow
    ...: from prefect.utilities.tasks import as_task
    ...: def hello_world():
    ...:     logger = prefect.context.get("logger")
    ...:     <http://logger.info|logger.info>("Hello, World!")
    ...: flow = Flow("hello-flow", tasks=[task(hello_world)])
    ...: if __name__ == "__main__":
    ...:     flow.run()
    ...: 
[2020-10-29 17:49:06] INFO - prefect.FlowRunner | Beginning Flow run for 'hello-flow'
[2020-10-29 17:49:06] INFO - prefect.TaskRunner | Task 'hello_world': Starting task run...
[2020-10-29 17:49:06] INFO - prefect.hello_world | Hello, World!
[2020-10-29 17:49:06] INFO - prefect.TaskRunner | Task 'hello_world': Finished task run for task with final state: 'Success'
[2020-10-29 17:49:06] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
upvote 1
n
It works, is exactly that! Thank you very much! 😀