https://prefect.io logo
Title
g

Garret Cook

07/17/2021, 2:19 AM
Is there any way to get task results in a flow state handler?
👀 1
maybe I can put them in context as I go
k

Kevin Kho

07/17/2021, 3:03 AM
Hey @Garret Cook, you can't modify the context. I think there are a couple of ways to achieve this, but could you tell me more about what you're trying to do?
g

Garret Cook

07/17/2021, 3:03 AM
I wanted to use a flow result handler to send a message
But I wanted to add some data to that message
Some of the flow results, you know, like success! 41 files transferred
that sort of thing, just some basic metadata about what happened and how
I’ve scoured google and the docs, tried it half a dozen ways, and just can’t seem to figure it out
k

Kevin Kho

07/17/2021, 3:11 AM
You may have to use FlowRunView here. Instantiate it inside the flow handler with
FlowRunView.from_flow_run_id(prefect.context.get("flow_run_id"))
inside the state handler. After that, you can use
*FlowRunView.get_all_task_runs*()
, which gives you a list of
TaskRunView
objects. Each
TaskRunView
object can be used to get the result using the
.get_result()
method. This is all new in 0.15.0 so I haven't tried it myself quite yet but I think it should work
g

Garret Cook

07/17/2021, 3:14 AM
interesting, ok
Is there a better pattern for doing this?
I just need to let accounting know whether it was pass/fail and how many uploads were moved through
k

Kevin Kho

07/17/2021, 3:17 AM
Is it just like a mapped task?
g

Garret Cook

07/17/2021, 3:17 AM
Right
k

Kevin Kho

07/17/2021, 3:19 AM
Check this . You can keep track of the state I guess and then feed that to another task for reporting
g

Garret Cook

07/17/2021, 3:20 AM
Gotcha, ok. Thank you
I think I saw you have a talk coming up, good luck, I hope it goes well
k

Kevin Kho

07/17/2021, 3:22 AM
Yep I do next week. Thanks!
That code snippet is slightly different because he needed task inputs instead of results for his reporting.