How can fetch and exec work inside parfor-loop

조회 수: 12 (최근 30일)
TIANTIANBABA
TIANTIANBABA 2021년 1월 17일
편집: TIANTIANBABA 2021년 1월 18일
The following is my code: when change parfor to for, everything works fine, but when in parfor-loop, it won't work.
Can someone know how to let exec and fetch work inside a parfor-loop ?Thanks.
sqlStr = 'select echo,wave from vibdata';
parfor m = 1:size(vs_list_temp,1)
DatabaseExpo(xx1,'','',xx2,xx3,m,sqlStr); % let everything do in DatabaseExpo function
end
function DatabaseExpo(loca1,loca2,loca3,loca4,loca5,m,sqlStr)
eval(['conn_' num2str(m) ' = database(loca1,loca2,loca3,loca4,loca5);'])
eval(['curs_' num2str(m) ' = exec(conn_' num2str(m) ',sqlStr);'])
dataObj = fetch(eval(['curs_' num2str(m)]));
data = dataObj.Data;
close(eval(['curs_' num2str(m)]));
close(eval(['conn_' num2str(m)]));
save(['dbrjs' num2str(m)],'data');
  댓글 수: 2
Edric Ellis
Edric Ellis 2021년 1월 18일
What's the error you are getting? Are you sure your database supports multiple concurrent connections? Rather than using eval , you ought to be able to simply something more like this:
parfor m = ..
out{m} = DatabaseExpo(..,m,..);
end
function data = DatabaseExpo(..)
conn = database(..);
curs = exec(conn, sqlStr);
dataObj = fetch(curs);
data = dataObj.Data;
close(curs);
close(conn);
end
TIANTIANBABA
TIANTIANBABA 2021년 1월 18일
편집: TIANTIANBABA 2021년 1월 18일
Error message: fetch UndefinedFunction. ..................
Reason: Undefine struct type input of fetch function.
My database is SQLITE, and each of my file is a "standalone" database. Each of my file (database) is very small, just aboute 2M. But I got so many files.
About eval, I use the same expression as you did in function DatabaseExpo(..), but MATLAB gives out error message. I am not familiar with database operation, and I don't know whether use eval to give different conn names and curs names can help, so you see my code with eval expression. But unfortunatelly, it doesn't help.
I have try parallel.pool.Constant(conn), but it doesn't help, either.
Driver of the database is: sqlite-jdbc-3.21.0.jar
Thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by