foreach %dopar% + RPostgreSQL

后端 未结 2 1758
囚心锁ツ
囚心锁ツ 2020-12-05 08:37

I am using RPostgreSQL to connect to a local database. The setup works just fine on my Linux machine. R 2.11.1, Postgres 8.4.

I was playing with the \'foreach\' with

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 09:26

    The following works and speeds up by ~ 1.5x over a sequential form. As a next step, I am wondering whether it is possible to attach a connection object to each of the workers spawned by registerDoMC. If so, then there would be no need to create/destroy the connection objects, which prevents from overwhelming the PostgreSQL server with connections.

    pgparquery <- function(i) {
    drv <- dbDriver("PostgreSQL"); 
    con <- dbConnect(drv, dbname='nsdq'); 
    lst <- eval(expr.01); #contains the SQL query which depends on 'i'
    qry <- dbSendQuery(con,lst);
    tmp <- fetch(qry,n=-1);
    dt <- dates.qed2[i]
    dbDisconnect(con);
    result <- list(date=dt, idreuters=tmp$idreuters)
    return(result)}
    
    id.qed.foreach <- foreach(i = 1588:3638, .inorder=FALSE, .packages=c("DBI", "RPostgreSQL")) %dopar% {pgparquery(i)}
    

    --
    Vishal Belsare

提交回复
热议问题