# Parallel errors in a Module, using pmap()

**URL:** <https://discourse.julialang.org/t/parallel-errors-in-a-module-using-pmap/54313>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 31, 2021, 7:17am UTC](https://discourse.julialang.org/t/parallel-errors-in-a-module-using-pmap/54313 "2021-01-31T07:17:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [January 31, 2021, 7:17am UTC](https://discourse.julialang.org/t/parallel-errors-in-a-module-using-pmap/54313/1 "2021-01-31T07:17:04Z")

</div>

I create a package and using pmap for parallel. Encountered many errors, when run `Pkg > test`.  
The package is as follows.  
file1: `/ParalAlg/src/ParalAlg.jl`

```julia
module ParalAlg
using Distributed
Distributed.addprocs(3)
@everywhere begin
      include("../src/para.jl") #include("para.jl") does not work
end

export 
    fast_alg

include("para_alg.jl")
end # module

```

file2: `/ParalAlg/src/para_alg.jl`

```julia
function fast_alg(x::Int)
    @show "fast_alg nprocs(): ", Distributed.nprocs() #why only 1 process here ???
    xs = collect(1:x) 
    ys = pmap(alg, xs) # error here, alg not defined
    return ys
end

```

file3: `/ParalAlg/src/alg.jl`

```julia
function alg(x::Int)
    return x^2
end

```

test file: `/ParalAlg/test/runtests.jl`

```julia
using ParalAlg 
@show ParalAlg.fast_alg(10)

```

The errors are as follows:

```julia
("fast_alg nprocs(): ", Distributed.nprocs()) = ("fast_alg nprocs(): ", 1)
ERROR: LoadError: UndefVarError: alg not defined
Stacktrace:
 [1] fast_alg(::Int64) at /Users/zhangliye/julia_dev/test/ParalAlg/src/para_alg.jl:5
 [2] top-level scope at show.jl:641
 [3] include(::String) at ./client.jl:457
 [4] top-level scope at none:6
in expression starting at /Users/zhangliye/julia_dev/test/ParalAlg/test/runtests.jl:2
ERROR: Package ParalAlg errored during testing

```

---

<div class="post-metadata">

**Author:** ![jawhite](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jawhite](https://discourse.julialang.org/u/jawhite)\
**Post date:** [February 12, 2021, 3:01am UTC](https://discourse.julialang.org/t/parallel-errors-in-a-module-using-pmap/54313/2 "2021-02-12T03:01:46Z")

</div>

I am having the same issue.

---

<div class="post-metadata">

**Author:** ![jawhite](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@jawhite](https://discourse.julialang.org/u/jawhite)\
**Post date:** [February 12, 2021, 4:37am UTC](https://discourse.julialang.org/t/parallel-errors-in-a-module-using-pmap/54313/3 "2021-02-12T04:37:00Z")

</div>

I don’t think this is the right approach. I found a tutorial on how to write packages with multiprocessing that seems to work: [Multiprocessing in Julia: writing a module - TechyTok](https://techytok.com/multiprocessing-in-julia-module/)
