我可以使用 R 中的 doParallel 在 Foreach 中仅使用一个 RODBC 连接吗?
Can I Use Only One RODBC Connection in Foreach using doParallel in R?
我知道我可以在每个 worker 中打开一个 SQL 服务器连接,但是,它会同时打开多个到服务器的连接。我工作的数据库管理员说我同时打开多个连接使用了太多系统资源,而我只需要使用一个连接。是否可以打开一个连接并将其传递给每个工作人员?我确实阅读了为什么无法在此处执行此操作的答案:,但我希望可能有新的解决方案或新的见解。
library(foreach)
library(doParallel)
cl <- makeCluster(detectCores() - 1)
clusterEvalQ(cl, {
library(RODBC)
Conn <- odbcConnect("SERVER_NAME")
})
foreach(iter=1:10, .noexport="Conn") %dopar% {
# Code block
}
我也需要这个,因为我正在创建一个临时 table 并且需要每个工作人员能够访问具有临时 table 的连接。否则,每个工作人员都会打开一个新连接,并且无法访问与临时 table 的连接。谢谢!
您将无法通过单个连接同时工作,即使您可以共享它。
如果您希望多个子进程(每个子进程都有自己的连接)查看相同的数据,请使用永久 table 或在驱动程序进程中创建的全局临时 table。例如
create table ##foo(...)
Global temporary tables are automatically dropped when the session
that created the table ends and all other tasks have stopped
referencing them. The association between a task and a table is
maintained only for the life of a single Transact-SQL statement. This
means that a global temporary table is dropped at the completion of
the last Transact-SQL statement that was actively referencing the
table when the creating session ended.
我知道我可以在每个 worker 中打开一个 SQL 服务器连接,但是,它会同时打开多个到服务器的连接。我工作的数据库管理员说我同时打开多个连接使用了太多系统资源,而我只需要使用一个连接。是否可以打开一个连接并将其传递给每个工作人员?我确实阅读了为什么无法在此处执行此操作的答案:
library(foreach)
library(doParallel)
cl <- makeCluster(detectCores() - 1)
clusterEvalQ(cl, {
library(RODBC)
Conn <- odbcConnect("SERVER_NAME")
})
foreach(iter=1:10, .noexport="Conn") %dopar% {
# Code block
}
我也需要这个,因为我正在创建一个临时 table 并且需要每个工作人员能够访问具有临时 table 的连接。否则,每个工作人员都会打开一个新连接,并且无法访问与临时 table 的连接。谢谢!
您将无法通过单个连接同时工作,即使您可以共享它。
如果您希望多个子进程(每个子进程都有自己的连接)查看相同的数据,请使用永久 table 或在驱动程序进程中创建的全局临时 table。例如
create table ##foo(...)
Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them. The association between a task and a table is maintained only for the life of a single Transact-SQL statement. This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.