필터 지우기
필터 지우기

Duplicate instances of methods executed while using parfeval and backgroundpool

조회 수: 4 (최근 30일)
I have the following function that sends regular updates
function countUp(q)
%countUp Counts up 1 every second
for i = 1:5
disp(i);
pause(1);
send(q, i);
end
end
The above function is called by the mlapp
properties (Access = private)
q = parallel.pool.DataQueue; % Description
L;
f;
end
methods (Access = private)
function myDisp(app, data)
log = ['Recvd data update: ', num2str(data)];
disp(log);
app.Label.Text = num2str(data);
if(data == 5)
cancel(app.f);
app.StartButton.Enable = true;
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
disp('Start button clicked');
app.StartButton.Enable = false;
app.L = afterEach(app.q, @(data) myDisp(app, data));
app.f = parfeval(backgroundPool,@countUp,0,app.q);
disp('Start button cb complete');
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
delete(app.L);
cancel(app.f);
app.StartButton.Enable = true;
end
First run returns
Start button clicked
Start button cb complete
Recvd data update: 1
Recvd data update: 2
Recvd data update: 3
Recvd data update: 4
Recvd data update: 5
Each time I start it it looks like duplicate instances of the function is executed
Start button clicked
Start button cb complete
Recvd data update: 1
Recvd data update: 1
Recvd data update: 2
Recvd data update: 2
Recvd data update: 3
Recvd data update: 3
Recvd data update: 4
Recvd data update: 4
Recvd data update: 5
Recvd data update: 5
It never looks like cancel(Future) is working as intended. What am I doing wrong?
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 6월 20일
For debugging purposes, I would do something like add a random number to the output. That would allow you to distinguish the case of the code executing twice (two different random numbers appear) from the case where the output is (for whatever reason) being displayed twice (same random number will appear for the pair of outputs)

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

채택된 답변

Jaynik
Jaynik 2024년 6월 20일
Hi Harish,
Before setting up a new listener with afterEach, ensure that any previous listeners are removed. This can be done by deleting app.L if it exists. You can edit the StartButtonPushed function as below:
function StartButtonPushed(app, event)
disp('Start button clicked');
app.StartButton.Enable = false;
% Delete existing listener before creating a new one
if ~isempty(app.L)
delete(app.L);
end
app.L = afterEach(app.q, @(data) myDisp(app, data));
app.f = parfeval(backgroundPool,@countUp,0,app.q);
disp('Start button cb complete');
end
Hope this helps!
  댓글 수: 1
Harish Ananthakrishnan
Harish Ananthakrishnan 2024년 6월 20일
편집: Harish Ananthakrishnan 2024년 6월 20일
Thanks for the response. Looks like I was deleting before cancelling
delete(app.L);
cancel(app.f);
I should have use this and it works. Any reason why one has to cancel a future before removing the callback?
cancel(app.f);
delete(app.L);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by