Animated simulation in GLMakie, launched from interface

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

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:

Now the issue:

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

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:

    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

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

1 Like

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

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 to run a computation that will terminate/restart at new user input, but that doesn’t seem suited to your use case here…

1 Like