Hi guys, good morning. :slightly_smiling_face: I'm...
# ask-community
a
Hi guys, good morning. 🙂 I'm starting to use prefect, and have a quick newbie question: What kind of scenario would I use task class instead of the task decorator? Just trying to understand it better.
⬆️ 1
👋 1
z
Hi Alex, great question! The task decorator just overrides the
.run()
method, so it's good for quick and dirty tasks. If you want to do something more in-depth or customizable, that's generally when you'd want to subclass. Does that make sense?
I've also found this page super useful as a more in-depth view of tasks-- might be helpful! https://docs.prefect.io/core/tutorials/task-guide.html
👍 3
e
I usually convert my decorated tasks into the task class, if I am passing parameters to the task that are known before the flow begins. For instance my database host address or username aren’t going to change based on what happens on the upstream tasks. By providing these values at the
Task
__init__()
call, I keep them out of the flow, while still utilizing their values. If I instead provided them with
.run()
, they wolud show up as
Constant
tasks, creating visual clutter in logs.
a
I think I see it now. Thanks Emre and Zachary 🙂
j
Does this mean that the
__init__()
of a task is executed before the flow actually runs?
e
Yep! Task initialization, like any object initialization, happens as soon as you call
Task()
, doesn’t even need to be in the
flow
. Prefect Tasks
.run()
, however, is delayed for execution until
flow.run()
happens and the tasks upstream dependencies are met.
👍 1