Asynchronous variable change

Hi, I am implementing biophysical simulation software with GUI using Blink.jl and js that controls Julia part of code. I want to be able to start at stop Julia algorithm at any iteration from GUI. I considered idea about checking global stopRequest variable at every Julia algorithm iteration. But when I try to launch test code Julia hangs.
I tried using @task or @async these are working with sleep(5), but not working when I replace sleep(5) with while a:

    function t1()
        while a
        end
        println("done 2")
    end
    a = true
    t = @task t1
    schedule(t) <-- hangs
    a = false
    a = true
    @async begin #hangs
        while a
        end
        println("done 1")
    end <-- hangs
    a = false

Then I tried multi-core processes, but still Julia hangs:

@everywhere function t1()
           while a
           end           
           println("done")
       end
@everywhere a = true
remotecall(t1,3)
@everywhere a = false   <-- hangs

Probably anybody could recommend better solution for such objective?

Those while loops don’t let the scheduler “breathe” since there are no yield points in them; adding a yield() call in them will at least allow the code to work correctly.

This of course will make your workers spin in a loop while waiting for something to do; if you have some heavy operation being done within the while loop then this is fine, but if not then you’ll be burning power for no reason.

If you can provide an MWE of how your simulation actually looks, then I can give better advice on a more useful solution for you. Otherwise my advice will be hand-wavy amd suboptimal.

1 Like

Thanks for reply @jpsamaroo, I saw yield() in Blink example, but I didn’t realize it’s importance at that time. Now it works. Yes, loop in simulation is calculation heavy so calculating while condition won’t be very frequent, thus not unnecessarily overload CPU. Otherwise it could be better to implement some other “wait” mechanism.