# How to use parpool worker again, after it times out?

8 views (last 30 days)

Show older comments

I am new to using Matlab's parpool. The only reason I am using it, is because I need to set a timeout on a call to int() in the symbolic toolbox, and this is the only way I found in Matlab to set a timeout on a function call. I am not using parpool to do any actual parallel processing. So that is why below I create only one worker.

I'll explain in words the problem, then give a MWE to reproduce it. I create a parpool using

parpool('local',1)

Then a function is called to integrate a problem, with a timeout of one minute using

fut = parfeval(@mupad_proc,0,the_integrand,the_variable);

ok = wait(fut, 'finished', 60); % Wait to finish

Then check if it timedout or not by checking the ok result. I noticed when the worker timeout, then each next call after that also timesout, when it should not.

I have thousands on integrals that I am processing this way, which means once one integral times out, then all the rest will also timeout because that worker is stuck/hanged.

What is the correct way to terminate the worker that times out and create a new worker in its place, so the next call do not hang?

Here is a MWE

function matlab_CAS()

%delete(gcp('nocreate'))

parpool('local',1) %create one worker

%%%%%%%%%%%%%%%

disp('trying integral which should not time out')

the_integrand = evalin(symengine,'sin(x)');

the_variable=evalin(symengine,'x');

fut = parfeval(@mupad_proc,0,the_integrand,the_variable);

ok = wait(fut, 'finished', 60); % Wait to finish

if ~ok

disp('Hanged. Terminated');

else

disp('completed on time');

end

%%%%%%%%%%%%%%%

disp('trying integral which will time out')

the_integrand = evalin(symengine,'(c + d*x)^3/sin(a + b*x)^3');

fut = parfeval(@mupad_proc,0,the_integrand,the_variable);

ok = wait(fut, 'finished', 60); % Wait to finish

if ~ok

disp('Hanged. Terminated');

else

disp('completed on time');

end

%%%%%%%%%%%%%%%

disp('trying first integral which should not time out, but it now hangs')

the_integrand = evalin(symengine,'sin(x)');

fut = parfeval(@mupad_proc,0,the_integrand,the_variable);

ok = wait(fut, 'finished', 60); % Wait to finish

if ~ok

disp('Hanged. Terminated');

else

disp('completed on time');

end

end

%this function is the one that the worker will run

function[]= mupad_proc(the_integrand,the_variable)

int(the_integrand,the_variable);

end

When I run the above, it prints on console the following

trying integral which should not time out

completed on time

trying integral which will time out

Hanged. Terminated

trying first integral which should not time out, but it now hangs

Hanged. Terminated

You see, how it times out on the 3rd call now, which it should not. This is because second call caused timeout. So I need to do some sort of clean up, to either restart the worker, or terminate it and start a new one?.

How does one handle such a problem in Matlab? Need to have symbolic toolbox to run the above. This is using Matlab 2021a on widnows 10.

UPDATE

I was trying things, and found that calling `cancel(...)` each time it times out seems to have solved the problem in the above test. I do not know now if this the correct way to handle this. I need to test this more on my main script. This is what it now looks like

fut = parfeval(@mupad_proc,0,the_integrand,the_variable);

ok = wait(fut, 'finished', 60); % Wait to finish

if ~ok

disp('Hanged. Terminated');

cancel(fut); %ADDED THIS

else

disp('completed on time');

end

Now, when I run the same function above, the output is

trying integral which should not time out

completed on time

trying integral which will time out

Hanged. Terminated

trying first integral which should not time out, now it no longer hangs. good.

completed on time

So now next call no longer hangs.

Is this the correct way to handle this issue?

### Accepted Answer

Edric Ellis
on 25 Jun 2021

##### 0 Comments

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!