How do I take a thread runner off the scheduler?


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

julia> c1 = Channel(32)

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

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

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


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


In the repl may be helpful


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.


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

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

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


You could also throw an exception to it:

struct TaskInterruptException <: Exception end
t = @schedule begin
    while true
        catch e
            if e isa TaskInterruptException
schedule(t, TaskInterruptException(); error=true)