[ library(timeout_simple) | Reference Manual | Alphabetic Index ]

timeout(+Goal, ++TimeLimit, +TimeOutGoal)

Run the goal Goal for a maximum of TimeLimit seconds.
Goal
Goal to run
TimeLimit
Time limit of Goal in seconds (integer or float)
TimeOutGoal
Goal to run on expiry of TimeLimit

Description

Goal is executed as if called via call(Goal), but only for a maximum of TimeLimit seconds. If the goal is still executing after TimeLimit, time-out occurs, the execution of the goal is terminated (via throw/1) and TimeOutGoal is executed. If the value of TimeLimit is 0 or 1.0Inf, no timeout is applied to the Goal.

Note that, if Goal is nondeterministic, execution flow may leave the scope of timeout/3 on success and re-enter on failure. In this case, only time spent within Goal will be counted towards the TimeLimit.

This predicate is implemented using alrm/vtalrm timer signals. These signals are alse needed for after-events, which will stop working when this predicate is being used. Also, this timeout predicate cannot be nested. If you need a timeout that can be nested, and is compatible with after-events, use lib(timeout).

Note that timeout/3 can be defined in terms of timeout/7 as:

    timeout(Goal, TimeLimit, TimeOutGoal) :-
        timeout(Goal, TimeLimit, TimeOutGoal, all_solution, _, _, _).

Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

Resatisfiable

If Goal is resatisfiable.

Examples

    ?- timeout((repeat,fail), 1.5, writeln(timed-out)). % time-out from infinite loop
    timed - out
    Yes (1.51s cpu)

    ?- timeout(repeat, 1.5, writeln(timed-out)), fail. % time-out from infinite loop
    timed - out
    Yes (1.51s cpu)

    ?- X=f(X), Y=f(Y), timeout(X=Y, 2, fail). % time-out from looping unification
    No (2.00s cpu)
    

See Also

timeout / 7