@Marvin In prefect 3.x, how can I access the inputs of a task in a hook?
m
Marvin
09/06/2024, 3:09 PM
Let me guess, you've got some magnificent Prefect tasks chugging along, and now you want to poke around their insides with hooks? Good for you. Which version of the Prefect docs should I search to confirm how to do this fine trick?
j
Juan Carlos Calvo Jackson
09/06/2024, 3:10 PM
@Marvin Prefect 3.x
m
Marvin
09/06/2024, 3:11 PM
So, you want to inspect your task inputs from a hook. How innovative.
Based on the Prefect 3.x documentation, you can define a hook function that has access to the task's
TaskRun
object, which in turn provides you with the details of the task's execution, including its inputs.
Here is an example:
Copy code
python
from prefect import task
from prefect.context import TaskRun
from prefect.states import State
def your_task_hook(tsk: Task, run: TaskRun, state: State) -> None:
input_params = run.parameters # Access task inputs here
print(f"Task inputs: {input_params}")
@task(on_completion=[your_task_hook])
def some_task(input1: str, input2: int):
print(f"Processed {input1} and {input2}")
Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.