Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@async doesn't seem to actually use a let-block for variables? #19468

Closed
quinnj opened this issue Dec 1, 2016 · 6 comments
Closed

@async doesn't seem to actually use a let-block for variables? #19468

quinnj opened this issue Dec 1, 2016 · 6 comments
Labels
bug Indicates an unexpected problem or unintended behavior parallelism Parallel or distributed computation regression Regression in behavior compared to a previous version

Comments

@quinnj
Copy link
Member

quinnj commented Dec 1, 2016

The docs for @async indicate that async-ed variables should be captured in a let-block, but actual use seems to contradict that.

julia> function async_add(x, y)
           println("adding $x + $y")
           return x + y
       end
async_add (generic function with 1 method)

julia> function async_wrong_value()
           y = x = 1
           while true
               sleep(1)
               println("going to add $x + $y")
               @async async_add(x, y)
               x += 1
           end
           return
       end
async_wrong_value (generic function with 1 method)

julia> async_wrong_value()
going to add 1 + 1
adding 2 + 1
going to add 2 + 1
adding 3 + 1
going to add 3 + 1
adding 4 + 1
going to add 4 + 1
adding 5 + 1

vs.

julia> function async_add(x, y)
           println("adding $x + $y")
           return x + y
       end
async_add (generic function with 1 method)

julia> function async_right_value()
           y = x = 1
           while true
               sleep(1)
               println("going to add $x + $y")
               let x=x
                   @async async_add(x, y)
               end
               x += 1
           end
           return
       end
async_right_value (generic function with 1 method)

julia> async_right_value()
going to add 1 + 1
adding 1 + 1
going to add 2 + 1
adding 2 + 1
going to add 3 + 1
adding 3 + 1
going to add 4 + 1
adding 4 + 1
@amitmurthy
Copy link
Contributor

Works as expected in 0.4. The bug has crept in 0.5.

@amitmurthy
Copy link
Contributor

expr = localize_vars(esc(:(()->($expr))), false)

and

julia/base/expr.jl

Lines 177 to 188 in 4a805b3

# wrap an expression in "let a=a,b=b,..." for each var it references
localize_vars(expr) = localize_vars(expr, true)
function localize_vars(expr, esca)
v = find_vars(expr)
# requires a special feature of the front end that knows how to insert
# the correct variables. the list of free variables cannot be computed
# from a macro.
if esca
v = map(esc,v)
end
Expr(:localize, expr, v...)
end

is where the localization is being performed.

@amitmurthy
Copy link
Contributor

The last set of changes w.r.t. this behavior was in #14562

@JeffBezanson do you think changes in #14562 is related to this issue?

@kshyatt kshyatt added docs This change adds or pertains to documentation parallelism Parallel or distributed computation labels Dec 1, 2016
@amitmurthy
Copy link
Contributor

This is a bug, not really a doc issue.

@amitmurthy amitmurthy added bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version and removed docs This change adds or pertains to documentation labels Dec 3, 2016
@bjarthur
Copy link
Contributor

closed by #19594 i believe

@amitmurthy
Copy link
Contributor

Yes. @async no longer implicitly localizes variables. http://docs.julialang.org/en/latest/stdlib/parallel.html#Base.@async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior parallelism Parallel or distributed computation regression Regression in behavior compared to a previous version
Projects
None yet
Development

No branches or pull requests

4 participants