# Asynchronous variable change

**URL:** <https://discourse.julialang.org/t/asynchronous-variable-change/25441>\
**Category:** Julia at Scale\
**Created:** [June 19, 2019, 11:57am UTC](https://discourse.julialang.org/t/asynchronous-variable-change/25441 "2019-06-19T11:57:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KestutisMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kestutisma/32/7835_2.png) [@KestutisMa](https://discourse.julialang.org/u/KestutisMa)\
**Post date:** [June 19, 2019, 11:57am UTC](https://discourse.julialang.org/t/asynchronous-variable-change/25441/1 "2019-06-19T11:57:35Z")

</div>

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

```julia
    function t1()
        while a
        end
        println("done 2")
    end
    a = true
    t = @task t1
    schedule(t) <-- hangs
    a = false

```

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

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

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [June 19, 2019, 12:47pm UTC](https://discourse.julialang.org/t/asynchronous-variable-change/25441/2 "2019-06-19T12:47:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KestutisMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kestutisma/32/7835_2.png) [@KestutisMa](https://discourse.julialang.org/u/KestutisMa)\
**Post date:** [June 25, 2019, 9:35am UTC](https://discourse.julialang.org/t/asynchronous-variable-change/25441/3 "2019-06-25T09:35:07Z")

</div>

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.
