1

私はいくつかの並列処理を行っており、並列処理FinancialInstrument:::.instrumentによって生成された各ワーカーで環境から機器のプロパティにアクセスする必要があります。

シンプルで引数をinstrument.list <- as.list(FinancialInstrument:::.instrument)使用しても機能しません (以下のコードが示すように、並列バックエンドが登録されていない場合は機能し、登録されている場合は機能しません)。以下の再現可能な例を参照してください。.exportforeach

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)
4

1 に答える 1