<@ULVA73B9P> Is there a way to limit the run time ...
# ask-marvin
m
@Marvin Is there a way to limit the run time of a flow? And if it exceeds the limit, stop/cancel the flow run?
m
Oh, the joy of dealing with time limits. It's not like I have all the eternity to wait for your tasks to finish. Anyway, in Prefect, there isn't a direct way to set a time limit for a flow run. However, you can set timeouts for individual tasks using the
timeout
parameter in the task decorator. Here's an example:
Copy code
python
from prefect import task, Flow
import time

@task(timeout=5)
def long_running_task():
    time.sleep(10)

with Flow("My Flow") as flow:
    long_running_task()

flow.run()  # This will raise a prefect.engine.signals.TIMEOUT signal
In this example, the
long_running_task
is set to timeout after 5 seconds. If the task runs longer than this, it will be interrupted and a
TIMEOUT
signal will be raised. Please note that this timeout is applied to each execution of the task, so if the task is retried, the timeout will apply to each retry separately. As for stopping or cancelling a flow run when it exceeds a certain time limit, you might need to implement a custom logic in your flow to handle this. For instance, you could have a task that checks the elapsed time and raises a
SKIP
signal to skip the remaining tasks if the time limit is exceeded. Remember, time is an illusion. Lunchtime, doubly so.
m
Can't I use the
timeout_seconds
parameter in flow? I want to cancel the flow itself not the tasks.
n
hi @Moe - i believe at this time you'd have to set
timeout_seconds
on the task decorator, we're aware that there's a problem with flows cancelling their children tasks - issue here
m
@Nate Doesn't
timeout_seconds
also available for @flow ? And I don't plan to use submit() my tasks are run sequentially.