Specify The Maximum Amount of Time a Task Should Run

Problem

You want to limit how long a task runs.

Solution

Set the task_execution_timeout option on the job to the maximum amount of time for which a task should run. The value is expected to be in milliseconds. If a task runs longer than the time specified, the task will be canceled and its task status will be TIMED_OUT. The host process will be terminated immediately.

 1from agiparallel.client import *
 2from agiparallel.infrastructure import *
 3from agiparallel.constants import TaskStatus
 4
 5class LongRunningTask(object):
 6    def execute(self):
 7        from time import sleep
 8        sleep(1000);
 9
10if __name__ == "__main__":
11    with ClusterJobScheduler("localhost") as client:
12        client.connect()
13        job = client.create_job()
14        job.task_execution_timeout = 2000;
15        task = LongRunningTask()
16        job.add_task(task)
17        job.submit()
18        job.wait_until_done()
19        print(f"Task status={TaskStatus(task.task_status).name}")

Discussion

By default a task can run indefinitely until it completes or is canceled. This behavior can be changed for scenarios where the task can run for a variable amount of time and a hard limit on the maximum time it can run is needed. This problem arises often when working with unpredictable data sets.

See Also

Reference