prefect icon indicating copy to clipboard operation
prefect copied to clipboard

Add `timeout_seconds` to task arguments

Open marvin-robot opened this issue 2 years ago • 6 comments

User Story

As a Prefect 2 user, I want to guarantee that a task doesn't hang too long on a particular run. I want to do this by specifying a timeout_seconds argument in the task decorator, just as I can for flows.

Opened from the Prefect Public Slack Community

enrique.sanchez: Hi Prefect! :slightly_smiling_face: I'm updating my flows from 1.X to 2.X. Is there a way to set a task timeout as we had in the previous versions? thanks a lot!

anna: You can currently set timeout_seconds on your flow decorator, so if you convert this task to a subflow, you can already solve the issue this way.

Setting timeout on a task is a reasonable request - let's open an issue to investigate whether we can add that - I believe this would be hard to do with async and various distributed task runners like Dask or Ray, but Michael can estimate that best - you can follow up on the issue. Thanks for bringing this up and until then you can set a timeout on a subflow

Original thread can be found here.

marvin-robot avatar Aug 29 '22 11:08 marvin-robot

cc @madkinsz since you know best whether this is possible

anna-geller avatar Aug 29 '22 11:08 anna-geller

This is possible now, though you have to apply it to each future, so it's not directly at the task level. But, you can pass a timeout kwarg to future.result() with the number of seconds to wait for completion of the task, if it doesn't return within that time a TimeoutError is raised.

import time

from prefect import flow, task


@task
def add_one(n):
    time.sleep(10)
    return n + 1


@flow
def my_flow():
    future = add_one.submit(5)
    result = future.result(
        timeout=5
    )  # Since the task will sleep for 10s this will raise a TimeoutError after 5s.


my_flow()

bunchesofdonald avatar Aug 29 '22 13:08 bunchesofdonald

Amazing, thanks a lot, that will do @bunchesofdonald!

anna-geller avatar Aug 29 '22 13:08 anna-geller

Note this will throw a timeout error in the flow when retrieving the result from the task but it won't actually fail the task as timed out. I think we could probably add this with little effort, mirroring the flow implementation. I do not recall why it was excluded in the first place cc @zangell44?

zanieb avatar Aug 29 '22 14:08 zanieb

thanks for looking at it and reopening Michael!

anna-geller avatar Aug 29 '22 14:08 anna-geller

This issue is stale because it has been open 30 days with no activity. To keep this issue open remove stale label or comment.

github-actions[bot] avatar Sep 29 '22 02:09 github-actions[bot]