# Cannot run parallel for loop when calling R code

**URL:** <https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017>\
**Category:** General Usage\
**Tags:** parallel, r\
**Created:** [August 16, 2020, 2:05am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017 "2020-08-16T02:05:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrickXie17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickxie17/32/23412_2.png) [@PatrickXie17](https://discourse.julialang.org/u/PatrickXie17)\
**Post date:** [August 16, 2020, 2:05am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017/1 "2020-08-16T02:05:14Z")

</div>

Hi, I am recently moving my projects from R to Julia, and part of the code still relies on some R packages. I encountered problems when putting the R code inside the parallel for loop by using `Threads.@threads`. Below is a minimal working example:

```julia
using RCall

Threads.@threads for i in 1:10
    R"a = rnorm(1)"
    @rget a;
    println(a)
end

```

Julia terminated automatically and I got the following error message:

```julia
***longjmp causes uninitialized stack frame*** : /path/.local/bin/julia terminated

signal (11): Segmentation fault
in expression starting at untitled-f2168f6cd01b7cefdedd9bf234089f00:3
***longjmp causes uninitialized stack frame*** : /path/.local/bin/julia terminated

```

My operating system is Ubuntu 18.04 running Julia 1.5.0

I was wondering if there is anything I can do to call R functions inside a for loop in Julia? Thanks!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 16, 2020, 2:11am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017/2 "2020-08-16T02:11:15Z")

</div>

I’m not sure, but I bet it’s R that’s not thread safe here, so you’re not going to be able to do this.

You could use multiprocessing (running in separate processes).

---

<div class="post-metadata">

**Author:** ![PatrickXie17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickxie17/32/23412_2.png) [@PatrickXie17](https://discourse.julialang.org/u/PatrickXie17)\
**Post date:** [August 16, 2020, 2:30am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017/3 "2020-08-16T02:30:56Z")

</div>

Yes using multi-processing works! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![PatrickXie17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickxie17/32/23412_2.png) [@PatrickXie17](https://discourse.julialang.org/u/PatrickXie17)\
**Post date:** [August 16, 2020, 2:47am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017/4 "2020-08-16T02:47:37Z")

</div>

It works when running on my computer, but I got the following errors when using the @distributed to initialize the for loop on a Linux server

```julia
Stacktrace:
 [1] sync_end(::Array{Any,1}) at ./task.jl:235
 [2] macro expansion at ./task.jl:254 [inlined]
 [3] #addprocs_locked#44(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}, ::typeof(Distributed.addprocs_locked), ::Distributed.LocalManager) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/cluster.jl:474
 [4] addprocs_locked at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/cluster.jl:445 [inlined]
 [5] #addprocs#43(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{,Tuple{}}}, ::typeof(addprocs), ::Distributed.LocalManager) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/cluster.jl:438
 [6] addprocs at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/cluster.jl:432 [inlined]
 [7] #addprocs#253 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/managers.jl:316 [inlined]
 [8] addprocs(::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.2/Distributed/src/managers.jl:315
 [9] top-level scope at /u/y227xie/ResearchDocuments/fpp_julia/fcp_mean_fast.jl:11
 [10] include at ./boot.jl:328 [inlined]
 [11] include_relative(::Module, ::String) at ./loading.jl:1094
 [12] include(::Module, ::String) at ./Base.jl:31
 [13] exec_options(::Base.JLOptions) at ./client.jl:295
 [14] _start() at ./client.jl:464

```

Do you have any idea what was happening? Thanks!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 16, 2020, 3:17am UTC](https://discourse.julialang.org/t/cannot-run-parallel-for-loop-when-calling-r-code/45017/5 "2020-08-16T03:17:05Z")

</div>

Can you give the code you use? Hard to know what’s wrong without knowing the code.
