Proposal: safe_map
function for tasks
#3492
-
IntroductionHey all I have been using Prefect for a bit and have started generalizing many of my General Formclass TaskResult(NamedTuple):
key: str
result: Any
class TaskError(NamedTuple):
key: str
error: Exception
@task
def mapped_task(key: str, some, other, params) -> Union[TaskResult, TaskError]:
try:
# do some work
return TaskResult(key, result)
except Exception as e:
return TaskError(key, e)
@task
def filter_types(objs, dtype):
# This could also just be the `FilterTask`
# https://docs.prefect.io/api/latest/tasks/control_flow.html#filtertask
return [obj for obj in objs if isinstance(obj, dtype)
# assume we have defined:
# keys = [...]
# other params
with Flow("example") as flow:
# process
results = mapped_task.map(keys, unmapped(some), unmapped(other), unmapped(params))
# split results and errors
errors = filter_types(results, TaskError)
results = filter_types(results, TaskResult)
# do work with errors
# do work with results ProposalI think this could potentially be done for me by adding a @task
def mapped_task(key: str, some, other, params) -> Any
# do some work
return result
with Flow("example") as flow:
results, errors = mapped_task.safe_map(
keys,
unmapped(some),
unmapped(other),
unmapped(params),
unique_parameters=["key"]
)
# do work with errors
# do work with results Note: I added the Use CasesThe most common use case for me would be to log the error in some non-Prefect-Server controlled database while continuing on with the rest of the flow. But a direct example where I think people would see direct value would be something like taking the errors and sending a Slack message alerting of which items failed to process during the map. Directly for me this is how I process thousands of images, and in general if we lose a couple images in the pipeline, we don't care, we really just want to be alerted of what percentage / how many we lost. (I.E. a slack message that says "image_processing failed on 20 images") Interplay with TriggersGiven the above use cases, this adds to the interplay with Looking forward to feedback and ideas. 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @JacksonMaxfield - I understand your use case but I'm not sure if this yet rises to a level that we would want to promote as first-class in the Prefect framework. My recommendation at this time would be to take your first example code and bundle it up as a custom function If you look through the Prefect codebase, we very rarely apply semantic meaning (or even binary splits) to task states; we view the objective of the library as providing first-class tools for working with a multitude of states (success and failed of course, but also retries, skips, scheduled, and a variety we haven't even released yet!). We've only taken an "opinion" when we see a clear and unambiguous pattern emerge. Consequently I think it's a little too early to automatically have a map operator that splits the world into success and failure, though I'd welcome seeing similar patterns from the community in case a consensus has emerged around this. I suspect, however, that people handle this through a mix of This is just my gut design sense at this time - to reiterate, I do understand your use case and objective, but I'd want to see clear consensus emerge before we codify it, as there are many ways to achieve this. |
Beta Was this translation helpful? Give feedback.
Hey @JacksonMaxfield - I understand your use case but I'm not sure if this yet rises to a level that we would want to promote as first-class in the Prefect framework. My recommendation at this time would be to take your first example code and bundle it up as a custom function
safe_map
which applies your logic in a reusable way.If you look through the Prefect codebase, we very rarely apply semantic meaning (or even binary splits) to task states; we view the objective of the library as providing first-class tools for working with a multitude of states (success and failed of course, but also retries, skips, scheduled, and a variety we haven't even released yet!). We've only taken an "opinio…