# Continue a loop in Julia when the time out

**URL:** <https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002>\
**Category:** General Usage\
**Created:** [September 3, 2020, 12:45pm UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002 "2020-09-03T12:45:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangqifan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangqifan/32/16249_2.png) [@yangqifan](https://discourse.julialang.org/u/yangqifan)\
**Post date:** [September 3, 2020, 12:45pm UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/1 "2020-09-03T12:45:40Z")

</div>

When I use the **DifferentialEquations.jl** to solve a ODEs, some parameters set is time-consuming. I have write a loop to solve the ODEs at diverse parameters. I want to early terminate the time-consuming parameters set, for example more than 60 seconds and execute the next loop.

I don’t know how to realize this functions. Is there any the useful package? 😀

**eventlet** package in **Python** is seems to work, but I know few about python. I want to know if any similar package in Julia. 😀

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 4, 2020, 8:17am UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/2 "2020-09-04T08:17:22Z")

</div>

Simplest solution maght be to use the `maxiters` flag in `solve(... ;maxiters=1000, ...)`. If you realy need a more more accurate timing you can use a [callback](https://diffeq.sciml.ai/stable/features/callback_functions/#The-Callback-Types) function:

```julia
using DiffEqBase, OrdinaryDiffEq

u0 = [1.,0.]
function fun(du,u,p,t)
   du[2] = -u[1]
   du[1] = u[2]
   sleep(0.1) # throttle the function
end
tspan = (0.0,10.0)
prob = ODEProblem(fun,u0,tspan)

struct Timer
    tstart::Array{Float64,1}
    runtime::Float64
    Timer(t) = new([0.0],t)

end

function (timer::Timer)(u,t,integrator) 
    if timer.tstart[1] == 0.0 
                timer.tstart[1] = time()
    end
    time() - timer.tstart[1] >= timer.runtime && return true
end

affect!(integrator) = terminate!(integrator)
cb = DiscreteCallback(Timer(60),affect!)
sol = solve(prob,Tsit5(),callback=cb)

```

---

<div class="post-metadata">

**Author:** ![yangqifan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangqifan/32/16249_2.png) [@yangqifan](https://discourse.julialang.org/u/yangqifan)\
**Post date:** [September 4, 2020, 9:29am UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/3 "2020-09-04T09:29:07Z")

</div>

Thanks a lot 😀  
I found the diverse parameters often get different iteration times.  
Besides, I found some parameter combinations really cause the solver too slow - it showed me _ **Interrupted. Larger maxiters is needed.** _ after a few hours, when i use the default` maxiters` flag. It also occupy too many memory. While, some parameter combinations may show me the solution a few seconds later. (both I used the `AutoTsit5(Rosenbrock23())` solver).

I have noticed `Base.Threads.@spawn` could created a task of `solve(...)`, and run in “backstage”. Then I can use` timedwait(() -> istaskdone(tsk1), max_runtime)` to timer the task. but I don’t know how to kill the task. Could you give me some advices? 🧐

```julia
using DifferentialEquations
u0 = [1.,0.]
function fun(du,u,p,t)
   du[2] = -u[1]
   du[1] = u[2]
   sleep(0.1) # throttle the function
end
tspan = (0.0,10.0)
prob = ODEProblem(fun,u0,tspan)

max_runtime = 10.0
tsk1 = Base.Threads.@spawn solve(prob, Tsit5())
timedwait(() -> istaskdone(tsk1), max_runtime)
if istaskdone(tsk1)
    sol = fetch(tsk1)
else 
    interrupt() # I don't know how to interrupt the task.
    sol = nothing
end
```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 4, 2020, 9:55am UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/4 "2020-09-04T09:55:09Z")

</div>

see this [post](https://discourse.julialang.org/t/how-to-kill-thread/34236/3)

```julia
...
if istaskdone(tsk1)
    sol = fetch(tsk1)
else 
    schedule(tsk1, ErrorException("stop"), error=true)
    sol = nothing
end

```

will make your example work, but personally I would avoid such a hack

---

<div class="post-metadata">

**Author:** ![yangqifan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangqifan/32/16249_2.png) [@yangqifan](https://discourse.julialang.org/u/yangqifan)\
**Post date:** [September 4, 2020, 12:37pm UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/5 "2020-09-04T12:37:11Z")

</div>

Thanks a lot 😀  
But it doesn’t seem to interrupt the task. The memory is still being used, and i almost could not do anything.

```julia
...
julia> run(`free -h`)
              total used free shared buff/cache available
Mem: 15Gi 2.3Gi 11Gi 42Mi 1.3Gi 12Gi
Swap: 15Gi 1.9Gi 14Gi
Process(`free -h`, ProcessExited(0))

julia> max_runtime = 10.0
10.0

julia> tsk1 = Base.Threads.@spawn solve(prob, Tsit5())
Task (runnable) @0x00007f3faf21bd00

julia> timedwait(() -> istaskdone(tsk1), max_runtime)
:timed_out

julia> if istaskdone(tsk1)
           sol = fetch(tsk1)
       else 
           schedule(tsk1, ErrorException("stop"), error=true)
           sol = nothing
       end

julia> sleep(40)

julia> run(`free -h`)
              total used free shared buff/cache available
Mem: 15Gi 8.7Gi 5.4Gi 38Mi 1.3Gi 6.4Gi
Swap: 15Gi 1.9Gi 14Gi
Process(`free -h`, ProcessExited(0))

julia> istaskdone(tsk1)
false

```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 4, 2020, 1:13pm UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/6 "2020-09-04T13:13:36Z")

</div>

that’s the reason why I would avoid such a way doing this. Why not using the callback as suggested?

---

<div class="post-metadata">

**Author:** ![yangqifan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yangqifan/32/16249_2.png) [@yangqifan](https://discourse.julialang.org/u/yangqifan)\
**Post date:** [September 4, 2020, 1:58pm UTC](https://discourse.julialang.org/t/continue-a-loop-in-julia-when-the-time-out/46002/7 "2020-09-04T13:58:25Z")

</div>

Thanks a lot. 😀  
I have thought it is a basic function, such as `ps`, `job`, `kill` in SHELL.  
Thank you once again. 😋
