"的foreach"循环:使用R中的所有核心(特别是如果我们在foreach循环内发送sql查询) [英] "foreach" loop : Using all cores in R (especially if we are sending sql queries inside foreach loop)

查看:172
本文介绍了"的foreach"循环:使用R中的所有核心(特别是如果我们在foreach循环内发送sql查询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用foreach来使用我CPU中的所有内核。赶上是我需要在循环内发送一个SQL查询。该脚本正常工作正常的'for'循环,但是当我将其更改为foreach时出现以下错误。
错误是:

$ p $ select:中断的系统调用
select:中断的系统调用
select:Interrupted system call
{:task 1 failed - expired MySQLConnection



我使用的代码是:

$ $ $ $ $ b $ library $($)$ b $ 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'CLOSEbetween 1 and 100 ,sep =)
t <-dbGetQuery(con,query)
}

尽管'foreach'在我的系统中可以正常工作,但是只有在sql查询的时候才会出错。是否有一种方法可以在'foreach'循环中发送sql查询?

将数据库查询移到循环之外,并锁定访问权限,以便不执行并行数据库查询。我认为这也会加快速度,因为你不会有并行磁盘访问,同时仍然可以进行并行处理。

含义(伪代码)
db =连接数据库
threadlock = lock();

parfor {
threadlock.lock
result = db query(把这里的所有数据都拉出来,因为你在加载的时候不能保持数据库的锁定而不能处理)
thread.unlock
处理结果数据(现在只是数据,而不是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?

解决方案

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). }

这篇关于&QUOT;的foreach&QUOT;循环:使用R中的所有核心(特别是如果我们在foreach循环内发送sql查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆