https://prefect.io logo
a

Alex Lopes

09/19/2019, 1:32 PM
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

Zachary Hughes

09/19/2019, 1:36 PM
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

emre

09/19/2019, 1:42 PM
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

Alex Lopes

09/19/2019, 1:43 PM
I think I see it now. Thanks Emre and Zachary 🙂
j

Jerry Thomas

09/20/2019, 5:06 AM
Does this mean that the
__init__()
of a task is executed before the flow actually runs?
e

emre

09/20/2019, 6:16 AM
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