如何在R中派生进程 [英] How to fork processes in R
问题描述
我试图了解R的多核软件包所实现的分叉系统.包示例为:
I'm trying to understand the forking system implemented by R's multicore package. The package example is:
p <- fork()
if (inherits(p, "masterProcess")) {
cat("I’m a child! ", Sys.getpid(), "\n")
exit(,"I was a child")
}
cat("I’m the master\n")
unserialize(readChildren(1.5))
,但在R交互式控制台中粘贴后似乎无效.有没有人举过在R的多核或并行包中使用fork()的示例?
but it doesn't seem to work when pasted in the R interactive console. Does anyone have an example of using fork() with R's multicore or parallel packages?
推荐答案
多核软件包为我工作"中的fork
示例;尝试example(fork)
.仅在非Windows系统上支持fork.
The fork
example in the multicore package 'works for me' ; try example(fork)
. fork is only supported on non-Windows systems.
我认为parallel
中的等效函数是mcparallel()
进行派生然后评估表达式,而mcollect()
则在完成后检索结果.所以
I think the equivalent functions in parallel
are mcparallel()
to fork and then evaluate an expression, and mcollect()
to retrieve the result when done. So
id = mcparallel({ Sys.sleep(5); TRUE })
立即返回,但进程正在运行,并且
returns immediately but the process is running, and
mccollect(id)
将在5秒后返回TRUE.除了分叉进程和主进程之间的集合外,没有其他通信.使用例如套接字来实现双向通信将是有趣的,并且不会带来太大的挑战.
will return TRUE after 5 seconds. There is no communication other than the collection between the forked and master process; it would be interesting and not too challenging to implement two-way communication using, e.g., sockets.
这篇关于如何在R中派生进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!