R parallel makeCluster() 在 Mac 上无限挂起

R parallel makeCluster() hangs infinitely on Mac

我在 Mac 上尝试使用 R 中的 parallel 包时遇到了一些问题。

下面是并行包的正常工作方式。

cl = makeCluster(2) # Using 2-core parallel as an example
# Your parallel code
stopCluster(cl)

当我 运行 这段代码时,cl = makeCluster(2) 无限挂起 。我试图解决它但失败了。我还参考了其他一些帖子。几个潜在的原因包括内存不足、安装错误等。它们似乎不是这里的问题,因为我重新启动了会话,重新安装了 R,但问题仍然存在。

我想问题出在 R 尝试连接到核心时的权限。这是我发现的。我用了future package来看具体连接cores的过程。附件是代码及其 return。

cl <- future::makeClusterPSOCK(2, verbose = TRUE)

Workers: [n = 2] ‘localhost’, ‘localhost’ Base port: 11303 Creating node 1 of 2 ... - setting up node Starting worker #1 on ‘localhost’: '/Library/Frameworks/R.framework/Resources/bin/Rscript' --default packages=datasets,utils,grDevices,graphics,stats,methods -e 'parallel:::.slaveRSOCK()' MASTER=localhost PORT=11303 OUT=/dev/null TIMEOUT=2592000 XDR=TRUE
Waiting for worker #1 on ‘localhost’ to connect back

问题是本地主机永远不会连接回来...

以下是我的会话信息。我希望这有帮助。

R version 3.5.1 (2018-07-02).
Platform: x86_64-apple-darwin15.6.0 (64-bit).
Running under: macOS High Sierra 10.13.6.

Matrix products: default.
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib.
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_3.5.1 parallel_3.5.1 tools_3.5.1 listenv_0.7.0 codetools_0.2-15 digest_0.6.16
[7] globals_0.12.2 future_1.9.0

有趣的是,相同的代码可以在我的旧 Mac 机器上运行(相同 OS,但硬件较旧)。我不知道这里发生了什么。任何帮助表示赞赏!谢谢!

Several potential reasons includes not enough memory, installation error, etc. They do not seems to be the problem here, as I restarted sessions, reinstalled R, but the problem remained.

正确,这类问题不应该在这里涉及。您展示的调用使用了 R 的基本内置功能(主要来自 'parallel' 包)并且涉及的内存使用量非常少。

I guess the problem is about the permission when R tried to connect to cores. [...]

parallel:makeCluster(2)future::makeClusterPSOCK(2) 都启动工作程序(使用 parallel:::.slaveRSOCK()),它们是在后台 运行 的独立 R 会话。主会话和这些工作人员通过套接字进行通信。所以,是的,可能是您的防火墙问题导致 R 无法打开这些端口。 (我对 macOS 的了解不够,无法解决这个问题)

通过设置outfile = NULL,您还将获得有关工人端发生的情况的信息。这是它工作时的样子:

> cl <- future::makeClusterPSOCK(1, outfile = NULL, verbose = TRUE)
Workers: [n = 1] ‘localhost’
Base port: 11306
Creating node 1 of 1 ...
- setting up node
Starting worker #1 on ‘localhost’: '/usr/lib/R/bin/Rscript' --default-packages=datasets,utils,grDevices,graphics,stats,methods -e 'parallel:::.slaveRSOCK()' MASTER=localhost PORT=11306 OUT= TIMEOUT=2592000 XDR=TRUE
Waiting for worker #1 on ‘localhost’ to connect back
starting worker pid=7608 on localhost:11306 at 14:46:57.827
Connection with worker #1 on ‘localhost’ established
- assigning connection UUID
- collecting session information
Creating node 1 of 1 ... done

PS。您只需要一名工作人员即可解决此问题。

经过数周的尝试,我终于解决了这个问题。我把我的答案放在这里。

问题实际上是由 macOS 中的一些未知防火墙问题引起的。解决方法是重新安装整个操作系统 ...我知道这听起来很愚蠢而且很麻烦,但之后问题就解决了。

这样做的动机是我碰巧注意到我无法访问主目录下的某些文件夹(我尝试使用 sudo 修改某些文件但未被授予访问权限) .这是我的个人笔记本电脑,不应该有这样的问题。然后我意识到这个 Mac 是从我的旧 Mac 同步而来的。同步过程可能会导致一些防火墙问题。