我正在使用 mclapply
multicore
中的函数包做并行处理。似乎所有启动的子进程都为 tempfile
给出的临时文件生成相同的名称。功能。即如果我有四个处理器,
library(multicore)
mclapply(1:4, function(x) tempfile())
将给出四个完全相同的文件名。显然,我需要临时文件不同,以便子进程不会覆盖彼此的文件。使用时
tempfile
间接地,即调用一些调用 tempfile
的函数我无法控制文件名。有没有解决的办法? R 的其他并行处理包(例如
foreach
)是否有同样的问题?更新 :从 R 2.14.1 开始,这不再是问题。
CHANGES IN R VERSION 2.14.0 patched:
[...]
o tempfile() on a Unix-alike now takes the process ID into account.
This is needed with multicore (and as part of parallel) because
the parent and all the children share a session temporary
directory, and they can share the C random number stream used to
produce the uniaue part. Further, two children can call
tempfile() simultaneously.
最佳答案
我相信 multicore
为每个子任务分拆一个单独的进程。如果这个假设是正确的,那么你应该可以使用 Sys.getpid()
“播种”临时文件:
tempfile(pattern=paste("foo", Sys.getpid(), sep=""))
关于r - 并行处理和临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5262332/