Way to terminate function that lasts more than 30 seconds?

Let’s say you’re randomly throwing darts at some range of parameter values, i.e.

  • a in linspace(0,1)
  • b from [15,20,40]
  • some_flag = [true,false]

However the black-box function you put all this into can take orders of magnitude longer for some configuration of variables:

foo(a,b,...,some_flag)

If you know foo takes O(seconds-to-mins), is there a simple way to limit the amount of time one instance of foo can take?


In pseudo-code,

# run foo for 30 seconds or terminate

@quickrun foo(a,b,...,someflag) 30
3 Likes

The current trick I’m using is from this 2015 google post:


example code below:

cur_pmap_complete = false
cur_run_count = 100
max_time = 60

 if !isnan(max_time)
  @schedule begin
    sleep(max_time)
    cur_pmap_complete || interrupt(workers())
  end
end

cur_progress = Progress(cur_run_count)

try
  pmap(cur_func, cur_progress, 1:cur_run_count)
catch cur_error
  isa(cur_error, RemoteException) || rethrow(cur_error)

  sleep(0.5)
  finish!(cur_progress)
end

cur_pmap_complete = true
1 Like

Is there a way to robustly do this?

Running into some finalizer bugs


edit: the error I get is the same as:

namely,

error in running finalizer: InterruptException()

and its more serious counterpart:

fatal error on 7: ERROR: InterruptException:
fatal error on Worker 7 terminated.
ERROR (unhandled task failure): EOFError: read end of file