# What exactly does @spawnat capture?

**URL:** <https://discourse.julialang.org/t/what-exactly-does-spawnat-capture/28865>\
**Category:** General Usage\
**Tags:** parallel\
**Created:** [September 17, 2019, 8:46pm UTC](https://discourse.julialang.org/t/what-exactly-does-spawnat-capture/28865 "2019-09-17T20:46:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [September 17, 2019, 8:46pm UTC](https://discourse.julialang.org/t/what-exactly-does-spawnat-capture/28865/1 "2019-09-17T20:46:48Z")

</div>

From the documents, I get the impression that the following statement

```julia
@spawnat 2 f(a, b)

```

will run if `f` is defined on the remote processes. `@spawnat` will capture the variable `a` and `b` automatically. However, this function/function arguments distinction seems rather weird to me. What will happen if `a` is also a function?

One example I am trying to play with is

```julia
using Distributed
addprocs()

@everywhere function do_work(f, jobs, results)
    while true
        x = take!(jobs)
        put!(results, f(x))
    end
end

function parallel_sum(f, xs)
    s = 0.0
    njobs = length(xs)
    jobs = RemoteChannel(()->Channel{Float64}(njobs))
    results = RemoteChannel(()->Channel{Float64}(njobs))
    for i = 1:njobs
        put!(jobs, xs[i])
    end
    for p in workers()
        @spawnat p do_work(f, jobs, results)
    end
    finished_jobs = 0
    while finished_jobs < njobs
        s += take!(results)
        finished_jobs += 1
    end
    return s
end

f(x) = exp(x)
xs = collect(1:10)
parallel_sum(f, xs)

```

I expect `f` to be sent to the remote process automatically, but it doesn’t. So what exactly does the document mean by “Create a closure around an expression and run the closure asynchronously on process p”?
