# Return values with @async

**URL:** <https://discourse.julialang.org/t/return-values-with-async/12923>\
**Category:** New to Julia\
**Created:** [August 6, 2018, 2:28am UTC](https://discourse.julialang.org/t/return-values-with-async/12923 "2018-08-06T02:28:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [August 6, 2018, 2:28am UTC](https://discourse.julialang.org/t/return-values-with-async/12923/1 "2018-08-06T02:28:18Z")

</div>

Hi everyone,

I would like to run two functions at the same time. A very basic example is

```julia
addprocs(3)

@everywhere begin
    function f()
        sleep(1)
        println("function f() is working")
        return [1 2]
    end

    function g()
        sleep(2)
        println("function g() is working")
        return [3 4 5]
    end
end

```

Then, I can perfect run them using `remotecall_fetch`

```julia
a,b = 0,0
@time begin
    a = remotecall_fetch(f,2)
    b = remotecall_fetch(g,3)
end
println(a)
println(b)

```

The result is:

```julia
        From worker 2:	function f() is working
	From worker 3:	function g() is working
  3.005970 seconds (957 allocations: 51.297 KiB)
[1 2]
[3 4 5]

```

I would like to use `@async`, but the end result is wrong:

```julia
a,b = 0,0
@time @sync begin
    @async a = remotecall_fetch(f,2)
    @async b = remotecall_fetch(g,3)
end
println(a)
println(b)

```

```julia
        From worker 2:	function f() is working
	From worker 3:	function g() is working
  2.031729 seconds (1.40 k allocations: 88.197 KiB)
0
0

```

what am I missing ?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 6, 2018, 4:54am UTC](https://discourse.julialang.org/t/return-values-with-async/12923/2 "2018-08-06T04:54:27Z")

</div>

I think there’s a local scope in there somewhere.

Try using `global`:

```julia
@time @sync begin
    @async global a = remotecall_fetch(f,2)
    @async global b = remotecall_fetch(g,3)
end

```

But you probably want to return a, b from a function.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [August 6, 2018, 1:44pm UTC](https://discourse.julialang.org/t/return-values-with-async/12923/3 "2018-08-06T13:44:45Z")

</div>

Only for reference. Using `@spawn` does the job

```julia
a,b = 0,0
@time begin
    a = @spawn f()
    b = @spawn g()
    a,b = fetch(a), fetch(b)
end
println(a)
println(b)

```

```julia
    From worker 4: function f() is working
    From worker 2: function g() is working
  2.029705 seconds (2.91 k allocations: 183.444 KiB)
[1 2]
[4 5 6]

```
