# Does \`@distributed\` return an array of futures?

**URL:** <https://discourse.julialang.org/t/does-distributed-return-an-array-of-futures/127992>\
**Category:** General Usage\
**Tags:** question, distributed\
**Created:** [April 11, 2025, 9:23pm UTC](https://discourse.julialang.org/t/does-distributed-return-an-array-of-futures/127992 "2025-04-11T21:23:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)\
**Post date:** [April 11, 2025, 9:23pm UTC](https://discourse.julialang.org/t/does-distributed-return-an-array-of-futures/127992/1 "2025-04-11T21:23:17Z")

</div>

The Julia manual ([Multi-processing and Distributed Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/distributed-computing/#Parallel-Map-and-Loops)) claims that `@distributed` without a reduction operator returns a an array of `Future`s:

> Using “outside” variables in parallel loops is perfectly reasonable if the variables are read-only:

```julia
a = randn(1000)
@distributed (+) for i = 1:100000
    f(a[rand(1:end)])
end

```

> Here each iteration applies `f` to a randomly-chosen sample from a vector `a` shared by all processes.
> 
> As you could see, the reduction operator can be omitted if it is not needed. In that case, the loop executes asynchronously, i.e. it spawns independent tasks on all available workers and returns an array of [`Future`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.Future) immediately without waiting for completion. The caller can wait for the [`Future`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.Future) completions at a later point by calling [`fetch`](https://docs.julialang.org/en/v1/base/parallel/#Base.fetch-Tuple%7BTask%7D) on them, or wait for completion at the end of the loop by prefixing it with [`@sync`](https://docs.julialang.org/en/v1/base/parallel/#Base.@sync), like `@sync @distributed for`.

But that doesn’t seem to be the case? I ran the following on my laptop (Julia version 1.11.4):

```julia
$ julia -p 2

julia> @everywhere f(x) = 2x

julia> a = randn(1000)
       @distributed (+) for i = 1:100000
         f(a[rand(1:end)])
       end
10268.871546420913

julia> @distributed for i = 1:100000
         f(a[rand(1:end)])
       end
Task (runnable, started) @0x000077c9cf892bd0

julia> ret = @distributed for i = 1:100000
         f(a[rand(1:end)])
       end
Task (runnable, started) @0x000077c9cf23b3a0

julia> typeof(ret)
Task

```

It looks to me like `@distributed` without a reducer returns a `Task`, not an array of `Future`s. Am I misunderstanding something?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [April 11, 2025, 9:25pm UTC](https://discourse.julialang.org/t/does-distributed-return-an-array-of-futures/127992/2 "2025-04-11T21:25:52Z")

</div>

Looks like `Task` is the type of the last object in the loop. How about trying something like `result[i] = f(a[rand(1:end)])` ?

Edit: In the documentation, there is an example of

```julia
a = zeros(100000)
@distributed for i = 1:100000
    a[i] = i
end

```

in page [Multi-processing and Distributed Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/distributed-computing/#Starting-and-managing-worker-processes)

---

<div class="post-metadata">

**Author:** ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)\
**Post date:** [April 11, 2025, 9:54pm UTC](https://discourse.julialang.org/t/does-distributed-return-an-array-of-futures/127992/3 "2025-04-11T21:54:08Z")

</div>

That also returns a task:

```julia
julia> a = zeros(100000);

julia> @distributed for i = 1:100000
           a[i] = i
       end
Task (runnable, started) @0x000077c9cf893b70

```
