Break function on time limit

I will probably make use of it. If I need to some change of behavior (probably will be needed as the Julia code I will be interrupting have calls to C) I will open a PR. I think it is a useful package for experiment scripts in experimental computer science (where you want the script to be written in Julia to benefit from JIT warming and better abstractions, but you may be calling third party code).

Here’s what I did to solve a similar problem. Probably not helpful for breaking out of a JuMP solver, but some of the discussion seems to reflect my use case of breaking out of a more custom process with a loop, if it’s still going after time_limit:

    TIME_LIMIT = Minute(2)
    start = Dates.now()
    time_elapsed = Minute(0)

    # Improve solution with local-moves until local optimum reached, or time-limit hit
    while !isnothing(local_move) && (time_elapsed < TIME_LIMIT)
        local_move = two_opt(tour, dist_matrix)
        if isnothing(local_move)
            println("Hit local optimum")
        else
            tour, dist_improved = local_move
            println("Improved tour distance by $dist_improved")
        end
        time_elapsed = Dates.now() - start
        if time_elapsed > Minute(TIME_LIMIT)
            println("Stopping local moves due to time elapsed > $TIME_LIMIT")
        end
    end

I ended up using a similar mechanism. Trying to use Distributed only brought headaches. Nobody answered my question about killing workers in a guaranteed way. In the end was far easier to create a new Exception type, propagate a deadline through a good part of code, and in key points call a function that checks the deadline and throws if it was violated. I solved the problem with the solver time limit by changing the solver time limit to exactly the remaining time before deadline when I call solve, and then checking for the deadline right after.

1 Like