# "kill" internal execution

**URL:** https://discourse.julialang.org/t/kill-internal-execution/73144
**Category:** General Usage
**Tags:** task
**Created:** [December 15, 2021, 1:28pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144 "2021-12-15T13:28:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 15, 2021, 1:28pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144/1 "2021-12-15T13:28:49Z")

</div>

Is it possible to “kill” the execution of a function under some condition?

The use case here would be for benchmarking. That is, I want to run several benchmarks, but I don’t want any of them to take more than some ammount of time, for example 1 minute. What I would like to do is to stop the execution of the function when that time is reached, without having to modify the internals of the function. Like a programmatically Control-C with exception handling.

Anything like that is possible?

(by the way, if someone knows if that is possible in Python, I’m comparing with some python stuff and that would be useful there as well).

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [December 15, 2021, 3:05pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144/2 "2021-12-15T15:05:11Z")

</div>

If you spawn each run in their own task, you can do `Base.throwto(t::Task, InterruptException())`, which is what Ctrl-C actually does.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 15, 2021, 5:27pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144/3 "2021-12-15T17:27:52Z")

</div>

For the records, this is how it works:

```julia
julia> function kill_at_max(fun, tmax)
           tsk = @task fun()
           schedule(tsk)
           t = 0
           while t < tmax
               t += 1
               sleep(1)
               if istaskdone(tsk)
                  return true
               end
           end
           try 
              Base.throwto(tsk, InterruptException())
           catch
           end
           return false
       end
kill_at_max (generic function with 1 method)

julia> wait() = sleep(3)
wait (generic function with 1 method)

julia> kill_at_max(wait, 4)
true

julia> kill_at_max(wait, 2)
false

```

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [December 15, 2021, 6:17pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144/4 "2021-12-15T18:17:17Z")

</div>

You can’t use `throwto` or `schedule(_, exception; error = true)` on an arbitrary task that is already started [Stop/terminate a (sub)task started with @async - #5 by jameson](https://discourse.julialang.org/t/stop-terminate-a-sub-task-started-with-async/32193/5).

If you are curious, see [https://github.com/JuliaLang/julia/pull/41270](https://github.com/JuliaLang/julia/pull/41270) for exactly when `schedule` can be called on a task that is already started. But I don’t think it’d be useful for the use case mentioned in the OP.

Probably the only way to do this reliably is to do the benchmark in a child process. (Or use something like Cassette to inject the cancellation token; but it’d change the program that is benchmarked.)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 15, 2021, 6:59pm UTC](https://discourse.julialang.org/t/kill-internal-execution/73144/5 "2021-12-15T18:59:15Z")

</div>

Yeah… maybe it is not worth the effort, I will try to keep the benchmark more mundane 🙂
