RPostgreSQL connections expired as soon as they start with doParallel clusterEvalQ

I am trying to set up a parallel task where every employee will need to make database queries. I try to configure each worker with a connection, as shown in this question , but every time I try, it returns <Expired PostgreSQLConnection:(2781,0)>for many people I registered.

Here is my code:

cl <- makeCluster(detectCores())
registerDoParallel(cl)

clusterEvalQ(cl, {
  library(RPostgreSQL)
  drv<-dbDriver("PostgreSQL")
  con<-dbConnect(drv, user="user", password="password", dbname="ISO",host="localhost")

})

If I try to start mine foreach, despite the error, it fails withtask 1 failed - "expired PostgreSQLConnection"

When I go into the postgres server state, it shows all active sessions that have been created.

I have no problem interacting with postgres from my main instance of R.

If I run

clusterEvalQ(cl, {
  library(RPostgreSQL)
  drv<-dbDriver("PostgreSQL")
  con<-dbConnect(drv, user="user", password="password", dbname="ISO",host="localhost")
  dbGetQuery(con, "select inet_client_port()")

})

. , foreach, .

Edit:

Ubuntu Windows, .

Edit:

Windows

+4
1

. , , , clusterEvalQ . , , dbGetQuery(con, "select inet_client_port()) . / , ( , - , ).

, , - , / . /, , , db. / master R, . , clusterExport, , .

, foreach. :

library(doParallel)
nrCores = detectCores()
cl <- makeCluster(nrCores)
registerDoParallel(cl)
clusterEvalQ(cl,library(RPostgreSQL))
clusterEvalQ(cl,library(DBI))

result <- foreach(i=1:nrCores) %dopar%
{
  drv <- dbDriver("PostgreSQL")
  con <- dbConnect(drv, user="user", password="password", dbname="ISO",host="localhost")
  queryResult <- dbGetQuery(con, "fetch something...")
  dbDisconnect(con)
  return(queryResult)
}
stopCluster(cl)

, foreach. - . , , / , . , .

+5

All Articles