Cancel parfeval with a uiprogressdlg
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sylvain Chupin
le 28 Août 2023
Commenté : Sylvain Chupin
le 31 Août 2023
Hello,
I'm making a UI that works in parallel and a uiprogressdlg is running while it's not finished.
I'm trying to make the cancel button of this uiprogressdlg do something but it's like nothing is happening when I push it.
I tried to put a line in me second loop above. It does nothing since when I push the button the code is doing the fetchNext line and when the fetchNext is done, every job are done...
I also add a listener to the button, but nothing happens when I push the cancel button...
Any hint how to stop all working jobs by hitting this cancel button ?
function testCancel()
clearvars
close all
clc
figT = uifigure('MenuBar','none','toolbar','none','NumberTitle','off','Name','main window');
uibutton(figT, 'push', 'Text', 'run', 'ButtonPushedFcn',{@run_Callback});
function run_Callback(~,~)
tic;
progressDlg = uiprogressdlg(figT, 'Title','running', 'Indeterminate','on', 'Cancelable', 'on');
pool = gcp("nocreate");
if isempty(pool)
parpool('local');
pool = gcp("nocreate");
end
future = parallel.FevalFuture();
for i=1:5
future(i) = parfeval(pool, @run,1, 5e7);
end
addlistener(progressDlg, 'CancelRequested', 'PostSet', @(src,event)disp('run canceled'));
for i=1:5
% if progressDlg.CancelRequested
% cancelRun(future);
% end
[~,tot] = fetchNext(future);
disp(tot);
end
delete(progressDlg);
toc;
end
function tot = run(nb)
tot=0;
for i=1:nb
tot = tot + rand(1);
end
tot = tot/nb;
end
function cancelRun(future)
disp('run canceled');
cancel(future);
end
end
0 commentaires
Réponse acceptée
Raymond Norris
le 28 Août 2023
When you start up the pool,
pool = gcp("nocreate");
if isempty(pool)
parpool('local');
pool = gcp("nocreate");
end
the parpool command will return a handle to the pool object, so there's no need to call gcp again.
pool = gcp("nocreate");
if isempty(pool)
pool = parpool("local");
end
Then again, since you want a pool to run (and start if it hasn't), I would replace all of this with just
pool = gcp;
This slight difference here is, I'm saying "give me a handle to a pool, if one doesn't exist, use the default profile." Where yours says, "...if one doesn't exists, use the local profile."
Note also that your tic/toc includes the pool startup (if it hasn't already started).
As you saw, you can't add a listener to the uiprogressdlg. I suspect that is why the CancelRequested property exists. It's perhaps the listener you should be using (and have commented out).
I would replace the for-loop with a while loop, as such (and it has the benefit that you don't care how many futures you add)
while true
if progressDlg.CancelRequested
cancelRun(future)
break
else
if any(~contains({future.State},'finished'))
[~,tot] = fetchNext(future);
disp(tot)
else
% No more future to fetch
break
end
end
end
If this doesn't work, I'll post the entire code, but I believe that was the only change I made. I should point out, what you can't do (and what you might be trying to do) is cancel the future you're currently listening to. That is, once you've checked that you haven't canceled the progress bar, MATLAB is blocking until it's finished fetching the next future. Before it checks for anymore futures, it'll check for the cancel again.
For this to be a bit more obvious, add
pause(rand*15)
In the run function before the for-loop. This will skew the runtimes a bit more for each of the parfevals (I'm assuming this is just an example you're showing us).
3 commentaires
Raymond Norris
le 30 Août 2023
while true
if progressDlg.CancelRequested
cancelRun(future)
break
elseif fetched == nbPar
break
elseif any(contains({future.State},'finished'))
[~,tot] = fetchNext(future);
fetched = fetched + 1;
disp(tot)
end
end
And since canceling finished tasks is a no-op,
while true
if progressDlg.CancelRequested || (fetched == nbPar)
cancelRun(future)
break
elseif any(contains({future.State},'finished'))
[~,tot] = fetchNext(future);
fetched = fetched + 1;
disp(tot)
end
end
Though this might be a bit more odd to read (since why would you cancel finished tasks?).
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!