# How to fetch a generator of @spawn correctly?

**URL:** <https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 27, 2023, 11:09pm UTC](https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670 "2023-01-27T23:09:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 27, 2023, 11:09pm UTC](https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670/1 "2023-01-27T23:09:18Z")

</div>

I have the below MWE for a simple function `test_serial()`. I tried to parallelize its `p1!()&p2!()` functions. However, it seems I am not using the `fetch` correctly. Could you please guide me here?

```julia
function p1!(x)
  x .+= 1;
end

function p2!(x)
    x .*= 2;
end

#
function test_serial()
    x =[zeros(10),zeros(10)]
    for i in 1:5
        p1!(x[1])
        p1!(x[2])
        x[1] .+= 1;
        p2!(x[1])
        p2!(x[2])
    end
  return x
end
x = test_serial()
#=
julia> x
[124.0, 124.0, 124.0, 124.0, 124.0, 124.0, 124.0, 124.0, 124.0, 124.0]
 [62.0, 62.0, 62.0, 62.0, 62.0, 62.0, 62.0, 62.0, 62.0, 62.0]
=#
function test_parallel()
    x =[zeros(10),zeros(10)]
    for i in 1:5
        tasks = [Threads.@spawn begin
                            p1!(x[i])
                            end for i = 1:2];
        fetch(tasks);
        x[1] .+= 1;
        tasks = [Threads.@spawn begin
                            p2!(x[i])
                            end for i = 1:2];
        fetch(tasks);
    end
  return x
end
x = test_parallel()
#=
julia> x
2-element Vector{Vector{Float64}}:
 [218.0, 218.0, 218.0, 218.0, 218.0, 218.0, 218.0, 218.0, 218.0, 218.0]
 [74.0, 74.0, 74.0, 74.0, 74.0, 74.0, 74.0, 74.0, 74.0, 74.0]
=#

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 30, 2023, 7:46pm UTC](https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670/2 "2023-01-30T19:46:08Z")

</div>

Any comment here please?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [January 30, 2023, 8:15pm UTC](https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670/3 "2023-01-30T20:15:04Z")

</div>

Seems like fetch only has sensible methods for a single task, channel, future etc. and a catch-all fallback for everything else defined as

```julia
fetch(@nospecialize x) = x

```

in task.jl (try running `@which fetch([1, 2])` in the repl).

Thus, the call `fetch(tasks)` will dispatch to this fallback and not wait on anything. To actually wait for the tasks within the vector, broadcasting would work: `fetch.(tasks)`.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [January 30, 2023, 10:02pm UTC](https://discourse.julialang.org/t/how-to-fetch-a-generator-of-spawn-correctly/93670/4 "2023-01-30T22:02:41Z")

</div>

Yes `fetch.(tasks)` works.  
Thank you very much!
