0
此代碼按預期工作:在doMC的foreach和dopar中調用其他包的注意事項是什麼?
library(dplyr)
data <- list(t1 = "hello world.", t2 = "bye world")
library(doMC)
registerDoMC(3)
res <- foreach(t = data) %dopar% {
print(sprintf("processing %s", t))
data.frame(text = t) %>%
dplyr::count(text)
}
print(res)
然而,這個代碼只是打印「處理世界你好」。和「處理再見世界」,然後掛起(沒有拋出異常)。上述
library(dplyr)
coreNLP::initCoreNLP()
data <- list(t1 = "hello world.", t2 = "bye world")
library(doMC)
registerDoMC(3)
res <- foreach(t = data) %dopar% {
print(sprintf("processing %s", t))
coreNLP::annotateString(t)$token
}
print(res)
代碼將如預期如果我改變%dopar%
到%do%
。
我不明白是什麼導致了這種行爲。爲什麼在%dopar%
內調用coreNLP函數會導致R掛起,但對其他軟件包正常工作?這是否與coreNLP對Java的依賴有關?
這裏是sessionInfo()
輸出:
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.0