4

I have a Julia script that repeatedly calls a C++ program to perform an optimization. The C++ program writes a text file, then I have Julia read the results and decide what to do next. The problem is that occasionally (maybe 1 in 1000+ times) the C++ program freezes (the optimization probably gets stuck), and my entire script hangs indefinitely, making it very difficult for the script to make it through all necessary program calls. Is there a way I can add a timeout, so that if the program has not finished within 10 minutes I can restart with a new guess value?

Simplified example:

for k = 1:10
    run(`program inputs`)
end

Desired:

max_runtime = 10*60 # 10 minutes
for k = 1:10
    run(`program inputs`,max_runtime)
end

Alternative:

max_runtime = 10*60 # 10 minutes
for k = 1:10
    deadline(function,max_runtime)
end

1 Answer 1

6

How about something like:

max_runtime = 10*60 # 10 minutes
for k = 1:10
    proc = spawn(`program inputs`)
    timedwait(() -> process_exited(proc), max_runtime)
    if process_running(proc)
        kill(proc)
    end
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help! I'm having trouble getting this to run. When I get to the timedwait call, I get an error message: ERROR: wrong number of arguments in anonymous at ... in timedwait at multi.jl:1552 in anonymous at no file:285 etc.` I'm not very competent with anonymous functions yet, is there an issue there?
Playing around a little more, I found that it works when I change x to (). I'm not sure exactly why this is, but thanks again for the help!
Oops,I did screw up that anonymous function call. I should have tested my answer but was in a hurry and wanted to get you something helpful sooner rather than later. My apologies and thanks for catching the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.