# How do I take a thread runner off the scheduler?

**URL:** <https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987>\
**Category:** New to Julia\
**Created:** [February 11, 2018, 6:18am UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987 "2018-02-11T06:18:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 11, 2018, 6:18am UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/1 "2018-02-11T06:18:58Z")

</div>

It’s easy to scheduler a runner… but how do I take them off schedule?

```julia
julia> c1 = Channel(32)
Channel{Any}(sz_max:32,sz_curr:0)

julia> function foo()
         while true
           data = take!(c1)
           println(data)
         end
       end
foo (generic function with 1 method)

julia> @schedule foo()
Task (runnable) @0x00007f93f0c85270

julia> @schedule foo()
Task (runnable) @0x00007f93f0c85cf0

```

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [February 11, 2018, 7:06am UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/2 "2018-02-11T07:06:21Z")

</div>

the line with `take!(c1)` looks like it should remove tasks when it runs, I think it may need to call `yield()` on the main thread to begin. I’m not sure whether those tasks are going into the channel or elsewhere though, or if yield will return given a while(true) loop

> ?schedule

In the repl may be helpful

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [February 11, 2018, 7:58am UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/3 "2018-02-11T07:58:28Z")

</div>

The line with `take!(c1)` just take something off the channel. Since the task has an an infinite loop so it will never end.

I can make an exit condition so that it breaks out of the loop when it sees a specific message from the channel. That works but when I have multiple runners then it is non-deterministic which one gets the message.

I thought it would be nice to have a `kill` function. Also some visibility into what tasks are in the scheduler and their respective state.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [February 11, 2018, 1:30pm UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/4 "2018-02-11T13:30:18Z")

</div>

The task is supposed to automatically exit when it ‘officially’ returns a value, but you can use a blocking call like yield() to keep it alive without holding up the main thread so much

```julia
 a() = return(rand(25));
b = Task(a);
schedule(b);
#in repl -> task completed
b.result = #returned val
schedule(b);
#error - task not runnable

b.status = :runnable;
schedule(b); ## Doesn't complain but probably isn't correct
schedule(b); ## Fatal error
```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [February 11, 2018, 1:42pm UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/5 "2018-02-11T13:42:44Z")

</div>

You could also throw an exception to it:

```julia
struct TaskInterruptException <: Exception end
t = @schedule begin
    while true
        try
            println("working")
            sleep(1)
        catch e
            if e isa TaskInterruptException
                return
            end
        end
    end
end
schedule(t, TaskInterruptException(); error=true)

```

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [August 25, 2022, 10:10am UTC](https://discourse.julialang.org/t/how-do-i-take-a-thread-runner-off-the-scheduler/8987/6 "2022-08-25T10:10:43Z")

</div>

Thanks!  
Could you show me the answer for another question?

How to terminate the running task(on any thread?), which is just calculation without any `wait` or `sleep`.

The scenarios is the result is out of time.
