Danny Vilela
12/17/2021, 9:51 PMtoday
template filled in a result location
or task target
string? I keep getting a `KeyError`:
from prefect.engine.results import LocalResult
# Create a new local result pointing to current working directory.
result = LocalResult(dir=".", location="{today}.txt")
assert result.location == "{today}.txt"
# This raises `KeyError: 'today'`.
result.write(value_="hello!")
I’m following this doc: https://docs.prefect.io/core/concepts/results.html#choose-a-serializer (under “Templating Result
locations”)Kevin Kho
with context...
Danny Vilela
12/17/2021, 9:55 PMTaskRunner
and it doesn’t seem to populate the context: is that just a limitation of TaskRunner
? Here’s a reproducible example:
from prefect import task
from prefect.engine.results import LocalResult
from prefect.engine.task_runner import TaskRunner
@task(result=LocalResult(location="{today}.prefect"))
def root_task():
return [1, 2, 3]
task_runner: TaskRunner = TaskRunner(task=root_task)
task_runner.run()
Produces:
[2021-12-17 13:55:05-0800] INFO - prefect.TaskRunner | Task 'root_task': Starting task run...
[2021-12-17 13:55:05-0800] ERROR - prefect.TaskRunner | Unexpected error: KeyError('today')
Traceback (most recent call last):
File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/runner.py", line 48, in inner
new_state = method(self, state, *args, **kwargs)
File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/task_runner.py", line 926, in get_task_run_state
result = self.result.write(value, **formatting_kwargs)
File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/results/local_result.py", line 106, in write
new = self.format(**kwargs)
File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/result/base.py", line 133, in format
new.location = new.location.format(**kwargs)
KeyError: 'today'
[2021-12-17 13:55:05-0800] INFO - prefect.TaskRunner | Task 'root_task': Finished task run for task with final state: 'Failed'
Out[1]: <Failed: "Unexpected error: KeyError('today')">
Kevin Kho
Danny Vilela
12/17/2021, 10:00 PMFlow
for now. Thanks @Kevin KhoKevin Kho
Kevin Kho
Danny Vilela
12/17/2021, 10:07 PMimport pendulum
import prefect
from prefect import task
from prefect.engine.results import LocalResult
from prefect.engine.task_runner import TaskRunner
@task(result=LocalResult(dir="."), checkpoint=True, target="{today}-{date:%Y}.prefect")
def root_task():
return [1, 2, 3]
with prefect.context(date=pendulum.today(), today=pendulum.today().strftime("%Y-%m-%d")):
task_runner: TaskRunner = TaskRunner(task=root_task)
task_runner.run()
Thanks @Kevin Kho!Kevin Kho