Search code examples
algorithmtimejuliajulia-jump

Set a time limitation on algorithm in Julia


I have a Julia code which runs for quite sometime. I want to run the code for 3 hours and then terminate it. Does anyone know what is the most efficient way to do this. Would appreciate any advice. Thanks


Solution

  • I recommend using Distributed to spawn your function as a new process and control it's time (I believe I have been answering similar question but I cannot find the answer).

    Here is the code:

    using Distributed
    function run_with_timeout(timeout::Int,f::Function, wid::Int)
        result = RemoteChannel(()->Channel{Tuple}(1));
        @spawnat wid put!(result, (f(),myid()))
        res = (:timeout, wid)
        time_elapsed = 0.0
        while time_elapsed < timeout && !isready(result)
            sleep(0.5)
            time_elapsed += 0.5
        end
        if !isready(result)
            println("Timeout! at $wid")
        else
            res = take!(result)
        end
        return res
    end
    

    You can use it like this (please note how external packages are imported):

    wid = addprocs(1)[1]
    @everywhere using DataFrames
    @everywhere function ff()
        sleep(2)
        #code fir making heavy computations
        #this is the place to write whatever you need
        return DataFrame(x=[1,2],y=[1,3])
    end
    

    Now let us run it. Note that the second returned value is the workerid to run computations (you might want to remove it):

    julia> run_with_timeout(10,() ->(try;ff();catch ee;dump(ee);end ),wid)
    (2×2 DataFrame
    │ Row │ x     │ y     │
    │     │ Int64 │ Int64 │
    ├─────┼───────┼───────┤
    │ 1   │ 1     │ 1     │
    │ 2   │ 2     │ 3     │, 2)
    

    If we give our code only 1 sec to execute it will fail:

    julia> run_with_timeout(1,() ->(try;ff();catch ee;dump(ee);end ),wid)
    Timeout! at 2
    (:timeout, 2)
    

    Finally, since the timeout occured the process should be killed (otherwise it would continue to execute):

    rmprocs(wid)