YD
03/01/2022, 6:14 PMZanie
03/01/2022, 6:18 PMdt = datetime.strptime(dt, '%Y-%m-%d')
will need to occur in a task, as will n_days = (start_date - dt).days
Kevin Kho
03/01/2022, 6:18 PMdt = datetime.strptime(dt, '%Y-%m-%d')
n_days = (start_date - dt).days
will not work because dt
doesn’t have a value yet until the Flow is run. You need to put these commands in tasks so that they are run when the Flow runs.
@Zanie was faster than me but I’ll just leave my answer which is the sameZanie
03/01/2022, 6:19 PMYD
03/01/2022, 6:20 PM@task(nout=2)
def get_dt():
dt = (datetime.now() - timedelta(days=3)).strftime('%Y-%m-%d')
x = 5
return x, dt
@task()
def t_1(x):
logger = prefect.context.get("logger")
<http://logger.info|logger.info>(x * 10)
@task()
def t_2(x):
logger = prefect.context.get("logger")
<http://logger.info|logger.info>(x * 100)
def main():
start_date = datetime.now(timezone('US/Pacific'))
schedule = Schedule(clocks=[CronClock('30 8 * * *', start_date=start_date)])
with Flow("dt_passing", schedule=schedule, executor=LocalDaskExecutor()) as flow:
x, dt = get_dt()
dt = datetime.strptime(dt, '%Y-%m-%d')
n_days = (start_date - dt).days
with case(n_days > 4, True):
t_1(x)
with case(n_days > 4, False):
t_2(x)
flow.run()
print(x, dt)
Error:
dt = datetime.strptime(dt, '%Y-%m-%d')
TypeError: strptime() argument 1 must be str, not GetItem
Kevin Kho
03/01/2022, 6:23 PMYD
03/01/2022, 6:24 PMn_days
should be calculated in a task ?
is the x value OK ?Kevin Kho
03/01/2022, 6:28 PMYD
03/01/2022, 6:42 PMflow.run()
print(x, dt)
I get
<Task: get_dt[0]> <Task: get_dt[1]>
is there a different way to print ?Kevin Kho
03/01/2022, 6:51 PMYD
03/01/2022, 7:19 PMmerge
work? it is not actually merging the data, but just wait for task to be done ?
is this valid flow ?
with Flow("conditional-branches") as flow:
val3 = some_task()
cond = check_condition()
with case(cond, True):
val1 = action_if_true()
with case(cond, False):
val2 = action_if_false()
val = merge(val1, val2, val3)
another_action(val)
Kevin Kho
03/01/2022, 7:34 PMYD
03/01/2022, 7:36 PMcase_true = True
, case_false = True
and then val = merge(case_true, case_false)
?Kevin Kho
03/01/2022, 7:42 PMmerge
takes care of merging task resultsThe merge will return the first real result it encounters, or None. If multiple tasks might return a result, group them with a list.