# GLMakie with blocking thread

**URL:** <https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600>\
**Category:** Visualization\
**Created:** [September 19, 2024, 1:10pm UTC](https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600 "2024-09-19T13:10:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonw1](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@simonw1](https://discourse.julialang.org/u/simonw1)\
**Post date:** [September 19, 2024, 1:10pm UTC](https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600/1 "2024-09-19T13:10:15Z")

</div>

Hello good people!

I am trying to plot and process data in Julia with GLMakie. The data is communicated via a gRPC call and as I am unable to find suitable Julia packages for handling this, I have a python module that handles the communication. I’m using PyCall for interacting with Python.  
The problem that I am bumping in to is that the Python service “eats” the thread I’m on, so I started that in a separate thread. When I do this, the Makie window becomes unresponsive. Do you have any pointers as to what may be the problem and how to navigate around that?  
MWE:

```julia
using GLMakie

function mwe()
    fig = GLMakie.Figure()
    gl = fig[1, 1] = GridLayout()
    Toggle(gl[1, 1], active = false)
    display(fig)
    sleep(1)
    Threads.@spawn begin
        while(true)
        end
    end
end

```

The Toggle will not work in the MWE above and the GUI will freeze.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 19, 2024, 1:24pm UTC](https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600/2 "2024-09-19T13:24:39Z")

</div>

This works on my PC, could it be, that you only run julia with one thread?  
Make sure Threads.nthreads() \> 1.  
If there’s only one thread, you will need a yield to allow any other task to work.  
Also, be aware that GLMakie isn’t threadsafe, so you can’t just update any plot from another thread!  
You will need to push the updates to a channel, or something like that:

```julia
function mwe()
   channel = Channel(Inf) do channel
       fig = GLMakie.Figure()
       gl = fig[1, 1] = GridLayout()
       ax = Axis(gl[2, 1])
       t = Toggle(gl[1, 1], active=false, tellwidth=false)
       splot = scatter!(ax, rand(Point2f, 10))
       display(fig)
       task = @async for update! in channel 
           update!(splot) # pass Makie objects in here
       end
       wait(task)
   end
       
   Threads.@spawn begin
       while true 
           push!(channel, (plot)-> begin 
               plot[1] = rand(Point2f, 10)
           end)
           sleep(1/25)
       end
   end
end

mwe()

```

---

<div class="post-metadata">

**Author:** ![simonw1](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@simonw1](https://discourse.julialang.org/u/simonw1)\
**Post date:** [September 19, 2024, 1:53pm UTC](https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600/3 "2024-09-19T13:53:09Z")

</div>

Thank you for the reply!  
That’s very interesting, I did double check and

```julia
julia> Threads.nthreads()
4

```

I wonder why it works on your system but freezes on mine.

---

<div class="post-metadata">

**Author:** ![simonw1](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@simonw1](https://discourse.julialang.org/u/simonw1)\
**Post date:** [September 19, 2024, 1:56pm UTC](https://discourse.julialang.org/t/glmakie-with-blocking-thread/119600/4 "2024-09-19T13:56:34Z")

</div>

Actually, the code you posted works for me, but not the mwe I posted myself.  
Thanks again!
