# How to interrupt async tasks?

**URL:** <https://discourse.julialang.org/t/how-to-interrupt-async-tasks/77745>\
**Category:** General Usage\
**Created:** [March 11, 2022, 12:00pm UTC](https://discourse.julialang.org/t/how-to-interrupt-async-tasks/77745 "2022-03-11T12:00:09Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 11, 2022, 12:50pm UTC](https://discourse.julialang.org/t/how-to-interrupt-async-tasks/77745/3 "2022-03-11T12:50:13Z")

</div>

`@async` returns a `Task` - you can interact with that, though note that interrupting running tasks from a different tasks is generally not a good idea. It’s usually better to write the tasks in a way that they check some condition from time to time to make sure they can shut down gracefully or have some waitable object that you can `close`.

EDIT: for some more context:

> [@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…

---

_[View the full topic](https://discourse.julialang.org/t/how-to-interrupt-async-tasks/77745)._
