# How to close a remote Timer?

**URL:** <https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295>\
**Category:** Julia at Scale\
**Tags:** parallel\
**Created:** [January 13, 2021, 6:06pm UTC](https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295 "2021-01-13T18:06:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Dengler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_dengler/32/20969_2.png) [@Christian\_Dengler](https://discourse.julialang.org/u/Christian_Dengler)\
**Post date:** [January 13, 2021, 6:06pm UTC](https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295/1 "2021-01-13T18:06:03Z")

</div>

I want to run mutliple procedures in parallel on a server in fixed time ntervals. The idea was to ,e.g., simply use “@spawnat :any” together with “Timer”, however, I cannot find a way to stop the Timer once its started. Does anyone know how to stop this?

```julia
# if started on a worker, how can I stop this? close(?)
unstoppable = fetch(@spawnat(:any, Timer((x)->println("Hello"), 0; interval = 10)))

```

The other way around, using a “Timer” to trigger "@spawnat :any"s, it can be stopped using close, but I would prefer to have the first approach running, as I have persistent data between each run.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 13, 2021, 8:50pm UTC](https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295/2 "2021-01-13T20:50:26Z")

</div>

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

```julia
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
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>

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dengler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_dengler/32/20969_2.png) [@Christian\_Dengler](https://discourse.julialang.org/u/Christian_Dengler)\
**Post date:** [January 14, 2021, 5:47am UTC](https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295/3 "2021-01-14T05:47:39Z")

</div>

Exactly what I was looking for, thanks a lot!

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 14, 2021, 7:47am UTC](https://discourse.julialang.org/t/how-to-close-a-remote-timer/53295/4 "2021-01-14T07:47:27Z")

</div>

I’m actually a big fan of Julia’s message passing facilities. Many concurrency problems can be solved with it.

If you want to tell an asynchronous process something, consider how to pass a message to it. 😉
