“foreach” Loop :使用 R 中的所有核心(特别是如果我们在 foreach 循环内发送 sql 查询)

发布于 2024-11-16 08:22:13 字数 835 浏览 1 评论 0原文

我打算使用“foreach”来利用 CPU 中的所有内核。问题是我需要在循环内发送一个 sql 查询。该脚本在正常的“for”循环中工作正常,但当我将其更改为“foreach”时,它会给出以下错误。 错误是:

select: Interrupted system call    
select: Interrupted system call    
select: Interrupted system call    
Error in { : task 1 failed - "expired MySQLConnection"

我使用的代码是:

library(foreach)
library(doMC)
library(RMySQL)
library(multicore)
registerDoMC(cores=6)
m <- dbDriver("MySQL", max.con = 100)
con <- dbConnect(m, user="*****", password = "******", host ="**.**.***",dbname="dbname")
list<-dbListTables(con)
foreach(i = 1:(length(list))%dopar%{
  query<-paste("SELECT * FROM ",list[i]," WHERE `CLOSE` BETWEEN 1 AND 100",sep="")
  t<-dbGetQuery(con,query)
}

虽然“foreach”在我的系统中出于所有其他目的工作正常,但仅在 sql 查询的情况下才会给出错误。有没有办法在“foreach”循环内发送 sql 查询?

I intend to use "foreach" to uitlize all the cores in my CPU. The catch is i need to send a sql query inside the loop. The script is working fine with normal 'for' loop, but it is giving following error when i change it to 'foreach'.
The error is :

select: Interrupted system call    
select: Interrupted system call    
select: Interrupted system call    
Error in { : task 1 failed - "expired MySQLConnection"

The code i used is :

library(foreach)
library(doMC)
library(RMySQL)
library(multicore)
registerDoMC(cores=6)
m <- dbDriver("MySQL", max.con = 100)
con <- dbConnect(m, user="*****", password = "******", host ="**.**.***",dbname="dbname")
list<-dbListTables(con)
foreach(i = 1:(length(list))%dopar%{
  query<-paste("SELECT * FROM ",list[i]," WHERE `CLOSE` BETWEEN 1 AND 100",sep="")
  t<-dbGetQuery(con,query)
}

Though 'foreach' is working fine in my system for all other purposes, it is giving error only in case of sql queries. Is there a way to send sql queries inside 'foreach' loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-11-23 08:22:13

我的建议是这样的:
将数据库查询移到循环之外,并锁定访问,这样您就不会执行并行数据库查询。我认为这也会加快速度,因为您不会进行并行磁盘访问,但仍然能够进行并行处理。

含义(伪代码)
db = 连接到数据库
线程锁=lock();

帕福{
线程锁.lock
结果 = 数据库查询(将所有数据拉到此处,因为在加载时无法在不锁定数据库的情况下进行处理)
线程解锁
处理结果数据(现在只是数据,而不是 sql 对象)。
}

My suggestion is this:
Move the database queries outside the loop, and lock access so you dont do parallel database queries. I think that will speed things up too, as you won't have parallel disk access, while still being able to do parallel processing.

Meaning (pseudo code)
db = connect to database
threadlock = lock();

parfor {
threadlock.lock
result = db query (pull all data here, as you cant process while you load without keeping the database locked)
thread.unlock
process resulting data (which is now just data, and not a sql object).
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文