0

我想生成多个并行 r 工作人员,这些工作人员不需要等待其他工作人员完成工作才能获得新工作。

require(snow)

# generate sockets
cl <- snow::makeSOCKcluster(3, type = "SOCK", outfile="")


# here is the function that will be run in parallel
myfunc <- function(x=2)
{
  print(x)
  Sys.sleep(time = x)
  output = sample(x = 1:150, size = 1)
  return(output)
}

# here are the arguments that needs to run in parallel
myfunc_argument <- c(5, 50, 150)

snow::clusterApply(cl = cl, fun = myfunc, x = myfunc_argument)
stopCluster(cl)

在这个例子中,第一个工人需要等待第三个工人得到一份新工作。

下一组输入将是当前运行的输出(即在此示例中为 1 到 150 之间的随机数)

4

0 回答 0