Parallel errors in a Module, using pmap()

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

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

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

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

test file: /ParalAlg/test/runtests.jl

using ParalAlg 
@show ParalAlg.fast_alg(10)

The errors are as follows:

("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
1 Like

I am having the same issue.

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