If I detect an error while processing data within a parfor loop, I would like to gracefully exit the program. Can I delete the parallel pool object using "delete" to shut down all of the workers before exiting?

 채택된 답변

Matt J
Matt J 2026년 7월 16일 0:47

1 개 추천

It sounds like a job for onCleanup.

댓글 수: 5

Clay Fulcher
Clay Fulcher 2026년 7월 16일 2:33
편집: Clay Fulcher 2026년 7월 16일 2:34
Thanks Matt. I'll give this a try. I'm not sure it will destroy all the independent workers in a parallel loop, but I'll give it a try. Do you know if simply deleting the parallel pool object will accomplish the destruction of the tasks being performed by all the independent workers in the parfor loop?
Matt J
Matt J 2026년 7월 16일 12:52
Do you know if simply deleting the parallel pool object will accomplish the destruction of the tasks being performed by all the independent workers in the parfor loop?
A simple error will do that. If even one of the workers hits an error, the whole loop aborts and all of the workers stop what they are doing. The only question at that point is whether you want the pool left open. So your code could look like this:
function output=runSomething(poolSize)
objPool = parpool(poolSize);
objCleanup=onCleanup( @() delete(objPool));
try
parfor i=1:N
end
output=...
catch
disp 'The loop failed. Aborting'
output=[];
return
end
end
This will completely close the pool whether the function runs to successful completion or not.
Matt J
Matt J 2026년 7월 16일 12:59
If even one of the workers hits an error, the whole loop aborts and all of the workers stop what they are doing.
Note that not all Parallel Computing Toolbox tools behave this way. With parfeval, for example, it is possible for some workers to keep going when others fail.
Catalytic
Catalytic 2026년 7월 16일 13:30
편집: Catalytic 2026년 7월 16일 13:32
A simple error will do that
Here's a demo of that. When worker #3 aborts with an error, you can see that all the other workers whose task is too long never complete it --
parpool(6)
runThis(0) %Short task. All workers except #3 complete
Worker 4 Completed
Worker 6 Completed
Worker 1 Completed
Worker 2 Completed
Worker 5 Completed
Error using test>runThis (line 11)
Worker 3 failed
runThis(30) %Only workers 4,5,6 complete
Worker 4 Completed
Worker 6 Completed
Worker 5 Completed
Error using test>runThis (line 11)
Worker 3 failed
function runThis(N)
t=[ N,N,0,0,0,0];
parfor i=1:6
if i~=3
pause(t(i))
disp("Worker " +i+" Completed")
else
error 'Worker 3 failed'
end
end
end
--
Clay Fulcher
Clay Fulcher 2026년 7월 17일 0:39
Thank you both, Matt and Catalytic. I appreciate the expertise and your time. I will implement the try/catch with onCleanup to clean everything up upon hitting an error in the parfor loop.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품

릴리스

R2025b

질문:

2026년 7월 15일 22:28

댓글:

2026년 7월 17일 0:39

Community Treasure Hunt

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

Start Hunting!

Translated by