# Way to terminate function that lasts more than 30 seconds?

**URL:** <https://discourse.julialang.org/t/way-to-terminate-function-that-lasts-more-than-30-seconds/12906>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 5, 2018, 12:50am UTC](https://discourse.julialang.org/t/way-to-terminate-function-that-lasts-more-than-30-seconds/12906 "2018-08-05T00:50:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 12:50am UTC](https://discourse.julialang.org/t/way-to-terminate-function-that-lasts-more-than-30-seconds/12906/1 "2018-08-05T00:50:44Z")

</div>

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,

```julia
# run foo for 30 seconds or terminate

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

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 3:17am UTC](https://discourse.julialang.org/t/way-to-terminate-function-that-lasts-more-than-30-seconds/12906/2 "2018-08-05T03:17:26Z")

</div>

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

- [https://groups.google.com/d/msg/julia-users/WOXrzOnbwoI/pQLriPa72dAJ](https://groups.google.com/d/msg/julia-users/WOXrzOnbwoI/pQLriPa72dAJ)

* * *

example code below:

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

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 6:46pm UTC](https://discourse.julialang.org/t/way-to-terminate-function-that-lasts-more-than-30-seconds/12906/3 "2018-08-05T18:46:27Z")

</div>

Is there a way to robustly do this?

Running into some `finalizer` bugs

* * *

**edit:** the error I get is the same as:

> <https://stackoverflow.com/questions/43816617/interrupting-busy-workers-in-parallel-julia>

namely,

```julia
error in running finalizer: InterruptException()

```

and its more serious counterpart:

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

```
