# Modify a remote object

**URL:** https://discourse.julialang.org/t/modify-a-remote-object/10612
**Category:** Julia at Scale
**Created:** [April 29, 2018, 10:08pm UTC](https://discourse.julialang.org/t/modify-a-remote-object/10612 "2018-04-29T22:08:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)
#### Post date: [April 29, 2018, 10:08pm UTC](https://discourse.julialang.org/t/modify-a-remote-object/10612/1 "2018-04-29T22:08:48Z")

</div>

Hello, I would like to send an object to a worker and modify the remote object. The following code did not work as expected.

```julia-repl
julia> addprocs(1)
1-element Array{Int64,1}:
 2

julia> n = 1
1

julia> remotecall_fetch(()->n, 2)
1

julia> @everywhere function add1(n)
        n += 1
        println("n = $n")
       end

julia> remotecall_fetch(add1,2,n)
	From worker 2:	n = 2

julia> remotecall_fetch(add1,2,n)
	From worker 2:	n = 2 # expected 3

julia> remotecall_fetch(add1,2,n)
	From worker 2:	n = 2 # expected 4

```

I was expecting the value of `n` to have increased in the worker.

Thanks!

---

<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: [April 29, 2018, 10:54pm UTC](https://discourse.julialang.org/t/modify-a-remote-object/10612/2 "2018-04-29T22:54:47Z")

</div>

> [@bbrunaud](#):
>
> I was expecting the value of n to have increased in the worker.

`n` is local (a parameter) to function `add1`, so global `n` will not be incremented (even if executed locally).

Not sure what you want, but this will increment global `n`:

```julia
@everywhere function add2()
    global n
    n += 1
    println("n = $n")
end

remotecall_fetch(add2,2)

```

---

<div class="post-metadata">

### Author: ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)
#### Post date: [April 30, 2018, 12:59am UTC](https://discourse.julialang.org/t/modify-a-remote-object/10612/3 "2018-04-30T00:59:31Z")

</div>

Hi, thanks for the quick response

I want to move an object to another worker and modify it in place iteratively. In your example, is `global n` the variable in worker 1 or worker 2?. In other words, I would like the function to take the variable `Main2.n` and modify it, where `Main2` is the `Main` module in worker 2.

---

<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: [April 30, 2018, 1:38am UTC](https://discourse.julialang.org/t/modify-a-remote-object/10612/4 "2018-04-30T01:38:47Z")

</div>

> [@bbrunaud](#):
>
> In your example, is global n the variable in worker 1 or worker 2?

worker 2.

> [@bbrunaud](#):
>
> I want to move an object to another worker and modify it in place iteratively.

Maybe something like:

```julia
addprocs(1)

@everywhere begin
    defineA(src) = global const A = src
    incrementA() = (A .+= 1)
    getA() = A
end

```

Test it out:

```julia
p = 2
B = zeros(Int,2,2)
remotecall_fetch(defineA, p, B)
remotecall_fetch(getA, p)
remotecall_fetch(incrementA, p)
remotecall_fetch(getA, p)
B

```

.  
You might also want to look at: [How to avoid repeated data movement between processes?](https://discourse.julialang.org/t/how-to-avoid-repeated-data-movement-between-processes/9835)

See also [ParallelDataTransfer](https://github.com/ChrisRackauckas/ParallelDataTransfer.jl), which provides helper functions for transferring data between worker processes.
