# How to kill Thread which is running in background in julia

**URL:** <https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606>\
**Category:** New to Julia\
**Tags:** question, multithreading, genie\
**Created:** [October 5, 2023, 11:32am UTC](https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606 "2023-10-05T11:32:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jayesh\_Patil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayesh_patil/32/51605_2.png) [@Jayesh\_Patil](https://discourse.julialang.org/u/Jayesh_Patil)\
**Post date:** [October 5, 2023, 11:32am UTC](https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606/1 "2023-10-05T11:32:13Z")

</div>

I have one project in that project there is process which executing some calculations . and while execution is running when user request to stop the execution of process then Django send request to julia in julia that process is assign to one thread which is running in background and i want to kill or terminate that thread ,so please help me to how to kill thread which is running in background

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [October 5, 2023, 11:38am UTC](https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606/2 "2023-10-05T11:38:31Z")

</div>

```julia
julia> task = Threads.@spawn sleep(100)
Task (runnable) @0x00007fcc8fa3c650

julia> Base.throwto(task, InterruptException())
ERROR: InterruptException:
Stacktrace:
 [1] try_yieldto(undo::typeof(identity))
   @ Base ./task.jl:931
 [2] throwto(t::Task, exc::Any)
   @ Base ./task.jl:943
 [3] top-level scope
   @ REPL[16]:1

julia> istaskfailed(task)
true

```

Given that `Base.throwto` is undocumented, I don’t think this is really a good, trustworthy solution though. It was the only one I could find.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [October 6, 2023, 8:20am UTC](https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606/3 "2023-10-06T08:20:39Z")

</div>

I usually use [`schedule(...; error=true)`](https://docs.julialang.org/en/v1/base/parallel/#Base.schedule) for that:

```julia
julia> task = Threads.@spawn sleep(100);

julia> task
Task (runnable) @0x00007f660297c650

julia> schedule(task, :stop, error=true)
Task (failed) @0x00007f660297c650
:stop
Stacktrace:
 [1] try_yieldto(undo::typeof(Base.ensure_rescheduled))
   @ Base ./task.jl:920
 [2] wait()
   @ Base ./task.jl:984
 [3] wait(c::Base.GenericCondition{Base.Threads.SpinLock}; first::Bool)
   @ Base ./condition.jl:130
 [...]

julia> istaskfailed(task)
true

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [October 6, 2023, 5:50pm UTC](https://discourse.julialang.org/t/how-to-kill-thread-which-is-running-in-background-in-julia/104606/4 "2023-10-06T17:50:34Z")

</div>

The best practice would be to introduce a stop condition into the running background process loop, and then satisfy that condition on interrupt.
