Excuting event listeners asynchronously

12 views (last 30 days)
Please run below
hedt = uicontrol('Style','edit','Callback',@edt_callback);
addlistener(hedt, 'String', 'PostSet', @listener_1);
addlistener(hedt, 'String', 'PostSet', @listener_2);
addlistener(hedt, 'String', 'PostSet', @listener_3);
function edt_callback(~,~)
disp('edt callback ends')
end
function listener_1(~,~)
pause(1)
disp('listener 1 ends')
end
function listener_2(~,~)
pause(2)
disp('listener 2 ends')
end
function listener_3(~,~)
pause(3)
disp('listener 3 ends')
end
it takes ~6 second and listener_3 results first, which has longest pause time.
>> test_script
listener 3 ends
listener 2 ends
listener 1 ends
edt callback ends
so, obviously, all listeners linked to a event shall be excuted in serial (on by one) in Matlab.
I hope to run all listeners asynchronously, i.e. in seperated thread.
what would be my option?
Thank you.

Accepted Answer

Shivansh
Shivansh on 5 Jan 2023
Hi,
I have few findings as listed below-
  • As of now all MATLAB listeners execute one at a time and all on the main MATLAB thread. When creating a listener there is no way for you to specify the asynchronous execution of a set of callbacks on different threads. Thank you for bringing up this interesting idea. Our team might consider it as a future enhancement where the creator of a set of listeners could specify whether they want to execute listeners asynchronously on different thread.
  • Considering your example of three independent listeners. The possible workaround can be to attach only one listener and execute other functions under that particular listener in multi-threaded fashion using “background pool” or “par pool”. I am attaching a code snippet for better insight.
hedt = uicontrol('Style','edit','Callback',@edt_callback);
addlistener(hedt, 'String', 'PostSet', @listener_1);
% addlistener(hedt, 'String', 'PostSet', @listener_2);
% addlistener(hedt, 'String', 'PostSet', @listener_3);
function edt_callback(~,~)
disp('edt callback ends')
end
function listener_1(~,~)
tic
f=listener_3(1);
g=listener_2;
pause(1)
disp('listener 1 ends')
disp(g);
disp(f);
toc;
end
function k=listener_2()
pause(2)
k="listener 2";
end
function k=listener_3(~)
pause(3)
k="listener 3";
end
In the above code snippet, I have called listener_2 and listener_3 under listener_1 itself. I have executed the code without using multithreading or parallel computing. So the code is taking 6 seconds to execute which can be seen in output.
But if I use background pool with ‘parfeval’ function. it is taking less time to execute the same task once the pool is ready.
hedt = uicontrol('Style','edit','Callback',@edt_callback);
addlistener(hedt, 'String', 'PostSet', @listener_1);
% addlistener(hedt, 'String', 'PostSet', @listener_2);
% addlistener(hedt, 'String', 'PostSet', @listener_3);
function edt_callback(~,~)
disp('edt callback ends')
end
function listener_1(~,~)
tic
f=parfeval(backgroundPool,@listener_3,1,1);
g=listener_2;
pause(1)
disp('listener 1 ends')
disp(g);
disp(fetchOutputs(f));
toc;
end
function k=listener_2()
pause(2)
k="listener 2";
end
function k=listener_3(~)
pause(3)
k="listener 3";
end
In the above code snippet, I am running listener 3 in background pool within the listener 1 itself and it is taking less time as compared to previous approach once the pool is prepared. The code will take more time initially due to pool setup, but it will execute faster once the pool is prepared. I have attached outputs of code obtained after two runs of same code.
First Run’s Output
Second run's output
We can see that code is taking only 3 seconds during second run.
I am also attaching links to certain documentations which can help you in gaining more insight into multithreading and parallel computing so that you can modify the code according to your use case.
I hope this helps.

More Answers (0)

Categories

Find more on Events in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!