# Animated simulation in GLMakie, launched from interface

**URL:** <https://discourse.julialang.org/t/animated-simulation-in-glmakie-launched-from-interface/129077>\
**Category:** General Usage\
**Tags:** glmakie\
**Created:** [May 16, 2025, 7:58pm UTC](https://discourse.julialang.org/t/animated-simulation-in-glmakie-launched-from-interface/129077 "2025-05-16T19:58:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 16, 2025, 7:58pm UTC](https://discourse.julialang.org/t/animated-simulation-in-glmakie-launched-from-interface/129077/1 "2025-05-16T19:58:23Z")

</div>

I’m developing an educational tool where the student will follow a chemical reaction. The tool starts with this interface:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/4/2478e3f625f4781d03087d9248d73950ba9d5685.png)

When the student clicks `Run`, a cool animations (should) start, particles start bouncing and reacting with each other, and all the plots are dynamically updated. At the end the result is something like:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffbbb53561ee15997c8d5d57ff74590287eb790d.jpeg)

### Now the issue:

The `Run` button executes a `simulate!()` function. If I run it from the command line:

```julia-repl
julia> MyChemicalSimulation.simulate!()

```

everthing works perfectly, and the simulation appears dynamically, all the plots are dynamically updated, as everything should be.

However, when the user **actually clicks** on the `Run` button, the simulation runs, but plots are only updated when the simulation finishes.

In both cases the `simulate!()` function is updating the same variables (global ones) that are using for plotting.

What can be wrong? Why when clicking the `Run` button, the call to the same function does not dynamically updates the plots?

The button click is implemented as:

```julia-auto
    on(buttons[2].clicks) do _
        simulate!()
        return nothing
    end

```

If it is impossible to give a hint without the actual code, it is here: [MyChemicalSimulation.jl/src/MyChemicalSimulation.jl at main · lmiq/MyChemicalSimulation.jl · GitHub](https://github.com/lmiq/MyChemicalSimulation.jl/blob/main/src/MyChemicalSimulation.jl)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 16, 2025, 8:20pm UTC](https://discourse.julialang.org/t/animated-simulation-in-glmakie-launched-from-interface/129077/2 "2025-05-16T20:20:04Z")

</div>

Oh, got it working, just by adding an `@async` in front of the `simulate!()` call 🙂

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [May 17, 2025, 12:48am UTC](https://discourse.julialang.org/t/animated-simulation-in-glmakie-launched-from-interface/129077/3 "2025-05-17T00:48:30Z")

</div>

Huh, that is very interesting. Perhaps this pauses the render loop? It seems plausible -

```julia
julia> on(but.clicks) do c
       println(objectid(current_task()))
       end
ObserverFunction defined at REPL[13]:2 operating on Observable{Any}(0)
<proceed to click the button>
0xc7607caea7622ca2
0xc7607caea7622ca2
0xc7607caea7622ca2

julia> objectid(current_task())
0x9a563c5e060d7045

```

so it is executing the ObserverFunction in a different task to the REPL / “main” task. Most likely this is the GLMakie render loop.

You can also use [`async_latest`](https://github.com/JuliaGizmos/Observables.jl/blob/4e78ca2f7c251cde6ceb900baec1e38a424cd9db/src/Observables.jl#L576-L601) to run a computation that will terminate/restart at new user input, but that doesn’t seem suited to your use case here…
