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)
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
1 Like

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

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);
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

You could also throw an exception to it:

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)
2 Likes

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.