Nuno
10/29/2020, 5:45 PMas_task
but, even with global scoped functions isn’t working as expected.
Current code while testing:
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.Zanie
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.
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
Nuno
10/29/2020, 5:52 PM