我甚至不知道可以像这样分叉进程。在玩了一下之后,我发现了sendChildStdin
函数,你应该检查一下。至少有一种方式可以表示儿童进程的信号。这里是一个例子:
f<- function() {
message<-scan(n = 1, quiet = TRUE, what='character')
return(message)
}
p <- mcparallel(f())
a <- 1
# The message shouldn't contain spaces and should end with a newline.
parallel:::sendChildStdin(p, "created\n")
mccollect(p)[[1]]
[1] "created"
不要误解我的意思;如果你打算大量使用R,R可能不是你想要的语言,但它可能适用于轻量级应用程序。
我曾在RStudio之前测试的代码,但它似乎工作,它的方式,是从成功中难以区分是失败。无论如何,它基本上不会等待过程。例如,这应该永远不会完成,但它确实(仅RStudio)
f<- function() {
message<-scan(n = 1, quiet = TRUE, what='character')
return(message)
}
p <- mcparallel(f())
# parallel:::sendChildStdin(p, "created\n")
mccollect(p)[[1]]
# character(0)
不同的进程不共享他们的内存,所以你的子进程在主进程中不能检测到变量'a'的创建。 – wush978