Error Found an interactive session. You cannot have multiple interactive sessions open simultaneously. To terminate the existing session, use 'delete(gcp('nocreate'))'
7 次查看(过去 30 天)
显示 更早的评论
Hello everybody,
I want to create a pool but Matlab returns that I have a pool opened. I make sure that if a pool is open close it before open the new one. I mean:
delete(gcp('nocreate')); %delete the current pool
poolobj = gcp;
if isempty(poolobj)
poolsize = 1;
else
poolsize = poolobj.NumWorkers;
end
parpool(poolsize,'IdleTimeout',Inf);
This is my code. I check if a pool is opened and then open a new one.
What is wrong? It always shows that issue.
Thanks!
Javi
1 个评论
采纳的回答
更多回答(1 个)
Hernan
2022-10-14
编辑:Hernan
2022-10-14
Hello Javier,
I arrived here looking for something else, but I think maybe what you wanted to do is something like this:
poolobj = gcp('nocreate'); % get the pool object, and do it avoiding creating a new one.
if isempty(poolobj) % check if there is not a pool.
poolsize = 1;
else
poolsize = poolobj.NumWorkers;
delete( gcp('nocreate')); % delete the current pool object.
end
parpool( poolsize, 'IdleTimeout',Inf); % create a new pool with the previous poolsize and new specs.
In that way your code works.
And what I was looking for was just to delete in case the current number of workers is different that the amount of workers that I want.
So, to replace a parpool( workers, 'IdleTimeout', idletimeout) at the begining of my code, that would drop the error here in subject, I defined the following function:
function [ poolobj ] = check_my_parpool( workers, idletimeout)
if nargin < 1
workers = 4; % default number of workers that I want.
end
if nargin < 2
idletimeout = 20; % default time in minutes of being idle before shut itself down.
end
poolobj = gcp('nocreate'); % get the pool object.
if ~isempty(poolobj) % check if there IS actually a pool:
if poolobj.NumWorkers ~= workers % it has a different number of workers???:
delete( poolobj); % delete the current pool object.
end
end
if isempty( gcp('nocreate')) % finally, if there is not a pool:
poolobj = parpool( workers, 'IdleTimeout', idletimeout); % create a new pool.
end
end
So, as example, add that function to your workspace, and then just replace:
parpool( 4);
with:
check_my_parpool( 4);
I hope this helps you,
Best,
Hernán.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!