Return the ith iteration value of a variable in a for loop when the broken from the outside using @async

phantom
Tue, 13 Jun 2023 21:04:28 +0000
New to Julia

Hi sorry if I am being dense here. I would like the option of terminating a for loop from outside the function. If the loop is completed it should return the value of the variable being modified. However if the loop is terminated prematurely it should return the value of a variable at that given iteration. Conceptually I guess something like.

function stoptest(a,b, rep= Ref(true))
    @async for i = 1:b
        rep[] || break
        @show a-=1
        sleep(8)
    end
    return(a,rep)
end
testvalue, cont = stoptest(10,3)

Needless to say this is not working. Firstly the loop is not terminating so it does not return the final value of 7 to testvalue if allowed to run. Secondly, if I break the loop early by setting cont[]=false, then somehow testvalue is assigned the value of 10 rather than the value at the ith iteration. Any thoughts on how this could be achieved would be greatly appreciated thanks!

8 posts - 4 participants