Sandeep Aggarwal
05/13/2020, 12:44 PMLaura Lorenz (she/her)
05/13/2020, 2:03 PMnew_state.result
(for just the value) or new_state._result
(for the whole Result object, which itself has a value
attribute). You don’t need to query to get it as it is already on that object! A couple useful docs links on the subject: https://docs.prefect.io/core/concepts/states.html#state-objects, https://docs.prefect.io/core/advanced_tutorials/task-guide.html#state-handlers
To your second question, IIRC state handlers should have access to prefect.context
, which has a number of auto-populated attributes during a flow run including task_name
, the whole list is here: https://docs.prefect.io/api/latest/utilities/context.html#contextSandeep Aggarwal
05/13/2020, 2:17 PMLaura Lorenz (she/her)
05/13/2020, 2:35 PMprefect.context.caches
directly in your state handlers after you cache the output from your GRPC task (https://docs.prefect.io/core/concepts/persistence.html#output-caching). Regarding getting the flow state via context, I think the context, if you import it in the logic of the handler, might have the flow object on it, though I’m not sure if it is totally hydrated with the states you need. I can poke around a little bit about that some more if the data dependency route is still a no-go!Sandeep Aggarwal
05/13/2020, 2:45 PMdef container_task_state_handler(task, oldstate, newstate):
# Do something with obj (output of create_object) here.
return newstate
@task
def create_object():
obj = # Hit GRPC API to get the object.
return obj
container_task = CreateContainer(
image_name="python:3.6",
state_handlers=[
container_task_state_handler
]
)
with Flow("Sample flow") as flow:
command_object = create_object()
container = container_task(command=command_object.command)
flow.run()
I checked the CreateContainer code and looks like the **kwargs are passed on to parent Task class which unfortunately accepts a fixed set of attributes.
I can try the context.caches hack if that works. That makes me think if it is possible to attach values to prefect.context that are available for whole flow run?
The documentation doesn't say that context contains flow object, but I can see if it is actually available at runtime though.Laura Lorenz (she/her)
05/13/2020, 2:57 PMflow.run
but not in a persistent way at runtime, and for you the thing you want to attach doesn’t exist until your first task runsSandeep Aggarwal
05/20/2020, 7:02 AMLaura Lorenz (she/her)
05/20/2020, 3:00 PMSandeep Aggarwal
05/20/2020, 4:10 PM