# How to interrupt fun(a,b) in a for loop after 10 seconds?

**URL:** <https://discourse.julialang.org/t/how-to-interrupt-fun-a-b-in-a-for-loop-after-10-seconds/112951>\
**Category:** General Usage\
**Created:** [April 15, 2024, 8:39am UTC](https://discourse.julialang.org/t/how-to-interrupt-fun-a-b-in-a-for-loop-after-10-seconds/112951 "2024-04-15T08:39:29Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [April 15, 2024, 12:39pm UTC](https://discourse.julialang.org/t/how-to-interrupt-fun-a-b-in-a-for-loop-after-10-seconds/112951/21 "2024-04-15T12:39:59Z")

</div>

The problem of terminating an async task has been discussed over here:

> [@Stop/terminate a (sub)task started with @async](https://discourse.julialang.org/t/stop-terminate-a-sub-task-started-with-async/32193/):
>
> Hello, I search a bit here and on the web and found out that obviously there is no “official” way to terminate a started task in julia yet. However, throw an Interruption error seems to work as a workaround for now. But now I would like to do it from within a function. Here is the example. The stating case is working but the stopping case is not. function switch\_timer(switch) if switch global t = @async while true sleep(1) end else @eval :(Base.throwto(t, InterruptException())) en…

The conclusion seems to have been

```julia
task = @async some_long_running_function()
sleep(5) # give it some time to run
schedule(task, InterruptException(), error=true) # send an interrupt exception

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-interrupt-fun-a-b-in-a-for-loop-after-10-seconds/112951)._
