How to close a remote Timer?

e.g. write a callback function that closes the timer when it receives a stop message:

using Distributed
nprocs() == 1 && addprocs(2);

@everywhere struct Stop end

rch = [RemoteChannel(()->Channel(1), 1) for _ in 1:nprocs()]

@everywhere function doit(rch) # callback function
    (timer) -> begin
        ch = rch[myid()]
        stop = isready(ch) && take!(ch) isa Stop
        stop ? close(timer) : println("Hello")
    end
end

then for example

julia> @spawnat 2 Timer(doit(rch), 0; interval=5);

julia>       From worker 2:     Hello
      From worker 2:    Hello
      From worker 2:    Hello
      From worker 2:    Hello
julia> put!(rch[2], Stop());

julia>
2 Likes