- parfeval : https://www.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html
- https://www.mathworks.com/help/parallel-computing/simple-parfeval-example.html
- https://www.mathworks.com/help/parallel-computing/asynchronous-parallel-programming.html?s_tid=CRUX_topnav
parallel timers not executing as expected
4 次查看(过去 30 天)
显示 更早的评论
I am trying to run two parallel timers. Below is a minimal example.
I would expect (or wish) that timerCallback2 is executed when timerCallback1 is in pause mode. But the function seems to be blocking. I also tried to add some drawnow calls.
Is there any way to execute multiple timer callbacks while another is still running, or is this intended behavior? Does MATLAB not process the event queue while another event is still running? Or are there different event queues for timer events and GUI events?
Thank you for your help!
timer1 = timer(Name="timer1", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback1, TasksToExecute=3, Period=1);
start(timer1)
timer2 = timer(Name="timer2", ExecutionMode="fixedSpacing", BusyMode="queue", TimerFcn=@timerCallback2, TasksToExecute=30, Period=0.1);
start(timer2)
function timerCallback1(~,~)
disp("start T1...")
pause(2)
disp("end T1...")
end
function timerCallback2(~, ~)
disp(" start T2...")
pause(0.05)
disp(" end T2...")
end
%% Output is:
% start T1...
% end T1...
% start T2...
% end T2...
% start T2...
% end T2...
% start T2...
% end T2...
% start T2...
% end T2...
% start T2...
% end T2...
% start T2...
% end T2...
% start T2...
% end T2...
% start T1...
% end T1...
% start T2...
% end T2...
% start T2...
% and so on....
0 个评论
采纳的回答
Pavan Sahith
2024-8-7
编辑:Pavan Sahith
2024-8-7
Hello Marco,
To run multiple timer callbacks in parallel in MATLAB, you can try using the 'parfeval' function from the Parallel Computing Toolbox. This approach allows for non-blocking execution of functions, which can be beneficial in scenarios where you need concurrent timer functionality.
You can refer to the following sample code and see if it helps :
% Initialize parallel pool
if isempty(gcp('nocreate'))
parpool;
end
% Create data queues for communication
dq1 = parallel.pool.DataQueue;
dq2 = parallel.pool.DataQueue;
% Define callbacks to handle incoming data
afterEach(dq1, @(data) disp(data));
afterEach(dq2, @(data) disp(data));
% Start parallel execution
f1 = parfeval(@timerTask1, 0, dq1);
f2 = parfeval(@timerTask2, 0, dq2);
function timerTask1(dq)
for i = 1:3
send(dq, "start T1...");
pause(2); % Simulate long processing
send(dq, "end T1...");
pause(1); % Period between executions
end
end
function timerTask2(dq)
for i = 1:30
send(dq, " start T2...");
pause(0.05); % Simulate shorter processing
send(dq, " end T2...");
pause(0.1); % Period between executions
end
end
Note that you need the Parallel Computing Toolbox for this approach to work.
Consider referring to the following MathWorks documentation to know more
Hope you find this helpful!
更多回答(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!