首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将FinancialInstrument:::.instrument导出到foreach worker并解压缩它

将FinancialInstrument:::.instrument导出到foreach worker并解压缩它
EN

Stack Overflow用户
提问于 2017-01-02 17:27:56
回答 0查看 57关注 0票数 1

我正在做一些并行处理,需要在并行处理产生的每个worker上访问FinancialInstrument:::.instrument环境中的仪器属性。

简单的instrument.list <- as.list(FinancialInstrument:::.instrument)和将.export参数与foreach一起使用不起作用(如下面的代码所示,当没有注册并行后端时,它可以工作,当它们注册时,它不起作用)。请参考下面的可重现示例:

代码语言:javascript
复制
library(FinancialInstrument)
library(parallel)
library(doParallel)
library(foreach)

currency(c("USD", "EUR")) # define some currencies
stock(c("SPY", "LQD", "IBM", "GS"), currency="USD") # define some stocks
exchange_rate("EURUSD") # define an exchange rate

ls_stocks() #get the names of all the stocks
ls_instruments() # all instruments

getInstrument("IBM")

instrument.list <- as.list(FinancialInstrument:::.instrument)

# First let's run this without parallel backend registered to show the code works
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            .instrument <- as.environment(instrument.list)
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }


# Now, let's register a parallel backend
cl <- makeCluster(2, outfile="log.txt")
registerDoParallel(cl)

# And by looking in log.txt file we see that .instrument environment is not functioning properly. How to make this work?
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            .instrument <- as.environment(instrument.list)
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }

stopCluster(cl)
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41424161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档