2017-01-02 3 views
1

Я занимаюсь параллельной обработкой и нуждаюсь в доступе к свойствам прибора от FinancialInstrument:::.instrument среды для каждого рабочего, порожденного параллельной обработкой.Экспорт FinancialInstrument :::. Инструмент для foreach работников и распаковка его

Простой 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) 

ответ

1

Я получил эту работу, используя функцию 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)