使用R中的异步期货追加全局表 [英] Appending a global table using asynchronous futures in R

查看:84
本文介绍了使用R中的异步期货追加全局表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个由异步运行并行进程产生的全局表.它们是完全独立的,但是它们应该追加到相同的全局变量中(这在R Shiny中是反应性的,因此一旦所有期货都完成了任务,我要么需要有一个回调函数-这会很好,但我不知道如何-,否则我需要随着新结果的不断更新而更新该表).

I am trying to create a global table that is produced by asynchronously running parallel processes. They are completely independent, however they should append to the same global variable (this is reactive in R shiny so I either need to have a call back function once all futures are done with their task -which would be very nice but I dont know how-, or I need to constantly update the table as new results come in).

我尝试了以下仅锁定的方法(可能是因为所有进程都分配了相同的变量,当我将'a'更改为'b'时它可以工作,但结果无用)

I tried the following approach which just locks (probably because all processes are assigning to the same variable, when I change 'a' to 'b' it works but then result is useless)

library("listenv")
library("future")
plan(multiprocess)
futureVals <- listenv()
options(future.globals.onMissing = "ignore")
a<-0
b<-0
for(i in 1:5){
  futureVals[[i]] <- futureAssign(x='a', value={
      a <- a+1
      print(a)
  })
}

futureVals2 <- as.list(futureVals)
print(a)

我如何实现这个目标?

推荐答案

将来(或其他并行的后台R工作者)在主R进程中为变量分配值是不可能.任何结果都需要返回作为值.这是R.(*)

It is not possible for future (or other parallel, background R workers) to assign values to variables in the master R process. Any results need to be returned as values. This is a fundamental property of all parallel/asynchronous processing in R.(*)

话虽如此,您可能对 https://rstudio.github感兴趣. io/promises/articles/shiny.html .

PS. (*)您对futureAssign()的期望似乎不正确.

PS. (*) Your expectations of futureAssign() seems to be incorrect.

这篇关于使用R中的异步期货追加全局表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆