# Break function on time limit

**URL:** https://discourse.julialang.org/t/break-function-on-time-limit/7376
**Category:** General Usage
**Created:** [November 29, 2017, 12:38am UTC](https://discourse.julialang.org/t/break-function-on-time-limit/7376 "2017-11-29T00:38:30Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 15, 2020, 5:54pm UTC](https://discourse.julialang.org/t/break-function-on-time-limit/7376/21 "2020-05-15T17:54:10Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Max\_Epstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max_epstein/32/12236_2.png) [@Max\_Epstein](https://discourse.julialang.org/u/Max_Epstein)
#### Post date: [June 9, 2020, 11:19pm UTC](https://discourse.julialang.org/t/break-function-on-time-limit/7376/22 "2020-06-09T23:19:17Z")

</div>

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`:

```julia
    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

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 9, 2020, 11:34pm UTC](https://discourse.julialang.org/t/break-function-on-time-limit/7376/23 "2020-06-09T23:34:45Z")

</div>

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.](https://discourse.julialang.org/t/should-julia-be-able-to-really-kill-a-worker/39564) 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.

[Previous page](https://discourse.julialang.org/t/break-function-on-time-limit/7376.md?page=1)
