导出 FinancialInstrument:::.instrument 到 foreach worker 并解包
Exporting FinancialInstrument:::.instrument to foreach workers and unpacking it
我正在进行一些并行处理,需要从并行处理产生的每个工作人员的 FinancialInstrument:::.instrument
环境访问仪器属性。
简单 instrument.list <- as.list(FinancialInstrument:::.instrument)
和使用 .export
参数与 foreach
不起作用(因为下面的代码显示它在没有注册并行后端时有效,而在它们注册时无效)。请参阅下面的可重现示例:
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)
我使用 worker 中的 assignInNamespace 函数使这个工作正常。下面是上述问题的工作代码:
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% {
assignInNamespace(".instrument", as.environment(instrument.list), "FinancialInstrument")
message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
}
stopCluster(cl)
我正在进行一些并行处理,需要从并行处理产生的每个工作人员的 FinancialInstrument:::.instrument
环境访问仪器属性。
简单 instrument.list <- as.list(FinancialInstrument:::.instrument)
和使用 .export
参数与 foreach
不起作用(因为下面的代码显示它在没有注册并行后端时有效,而在它们注册时无效)。请参阅下面的可重现示例:
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)
我使用 worker 中的 assignInNamespace 函数使这个工作正常。下面是上述问题的工作代码:
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% {
assignInNamespace(".instrument", as.environment(instrument.list), "FinancialInstrument")
message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
}
stopCluster(cl)