# Easy way to send custom function to distributed workers?

**URL:** <https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118>\
**Category:** Julia at Scale\
**Created:** [March 20, 2019, 6:41pm UTC](https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118 "2019-03-20T18:41:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickeubank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nickeubank/32/3878_2.png) [@nickeubank](https://discourse.julialang.org/u/nickeubank)\
**Post date:** [March 20, 2019, 6:41pm UTC](https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118/1 "2019-03-20T18:41:19Z")

</div>

Is there a way to pass a function to workers? e.g.

```julia
using Distributed
addprocs(4)

function my_func(x)
    return x ^ 2
end

[send my function to everyone]

pmap(my_func, 1:100)

```

I recognize I can do something like:

```julia
@everywhere begin
    function my_func(x)
        return x ^ 2
    end
end

```

But since functions are first class objects (right?) seems like I should just be able to pass them around like data.

I tried using `ParallelDataTransfer.jl`, as suggested [here](https://discourse.julialang.org/t/send-a-function-to-worker-processes/20696), but like @gdkrmr I can’t figure it out and the package has no docs.

For example, I tried:

```julia
@passobj 1 workers() my_func

```

But that didn’t seem to work…

---

<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:** [March 21, 2019, 12:53am UTC](https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118/2 "2019-03-21T00:53:08Z")

</div>

I think the issue here is that you want define _methods_ (not just the function) on the workers.

I’m not sure how robust this is but it seems to work for your simple example:

```julia
function my_func(x)
    #println("Calculating my_func($x)")
    return x ^ 2
end

using Distributed
addprocs(4)

@everywhere workers() @eval function my_func end

for m in methods(my_func)
    @everywhere workers() @eval $m
end

pmap(my_func, 1:100)

```

---

<div class="post-metadata">

**Author:** ![nickeubank](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nickeubank/32/3878_2.png) [@nickeubank](https://discourse.julialang.org/u/nickeubank)\
**Post date:** [March 21, 2019, 1:20am UTC](https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118/3 "2019-03-21T01:20:08Z")

</div>

ahhhhh, right… methods / functions. I forgot about that distinction.

---

<div class="post-metadata">

**Author:** ![bojeryd91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bojeryd91/32/42622_2.png) [@bojeryd91](https://discourse.julialang.org/u/bojeryd91)\
**Post date:** [September 12, 2022, 8:35pm UTC](https://discourse.julialang.org/t/easy-way-to-send-custom-function-to-distributed-workers/22118/4 "2022-09-12T20:35:07Z")

</div>

Is this the most up-to-date solution? I’ve spent hours trying to find a oneliner, not having to iterate over the methods.

But I’m grateful that this works, thanks!
