https://prefect.io logo
Title
c

Chris O'Brien

05/04/2020, 3:56 AM
Hi Prefect Team, I would love your thoughts on if this is the Prefect way on doing this. I have a scheduled flow where we need a
run_date
that is yesterday UTC time. I would love to use the prefect context to do this and this worked for me, but is there a better way?
c

Chris White

05/04/2020, 3:12 PM
Hi @Chris O'Brien - modifying
context
within a Task is not recommended, and
context
should be treated as read-only (unless you are adding values to context through environment variable or the API). Why do you need to add this to context? You can always compute it within any task, as you did above:
run_date = prefect.context.date.date() - datetime.timedelta(days=1)
c

Chris O'Brien

05/04/2020, 10:43 PM
Thanks @Chris White, we have multiple different load tasks that all require this same date value.
Rather than calculate each time, and introduce another place this could go wrong, I would rather do it once for all the tasks to access. Similar in style to how you make
date
available via context
c

Chris White

05/04/2020, 10:52 PM
Gotcha gotcha; in that case I recommended creating a single task that computes this date and returns it as a piece of data for downstream tasks to consume directly from a kwarg
c

Chris O'Brien

05/04/2020, 10:54 PM
Ahhhh cool that was our original implementation