Disp in background thread

32 views (last 30 days)
Is it possible to disp some text to main console while executing code in background? I have an infinite function that is started by parfeval but it's difficult to track what's going on there and simply putting disp in code shows nothing (I guess that's beceause parallel worker has its own console output).

Accepted Answer

Edric Ellis
Edric Ellis on 16 Mar 2018
You could use parallel.pool.DataQueue together with afterEach to achieve this.
q = parallel.pool.DataQueue();
afterEach(q, @disp);
f = parfeval(@myFcn, 0, q);
function myFcn(q)
for idx = 1:100
pause(3);
send(q, sprintf('Iteration: %d at time: %s', idx, string(datetime)));
end
end

More Answers (1)

Michael Schlagmüller
Michael Schlagmüller on 23 Nov 2022
We have a related issue here.
We have a .NET assembly which has a callback
function DeviceLogger(level, message)
disp(message)
end
which is registered via
NET.addAssembly('DeviceAssembly');
DeviceAssembly.setLogger(@DeviceLogger);
The issue is that our customers using our assembly can collide with writing on the console, e.g., via
while(true)
pause(0.1)
disp('The customer outputs something to the console.')
DeviceAssembly.myfunc()
end
This freezes Matlab most often when the DeviceLogger is called. When the user does not do any disp messages or the user disp messages do not collide with the logger disp messages - everything works fine.
It seems from my testing that when there are two disp functional calls at the same time (one from the C# and one from the Matlab main thread) Matlab freezes.
The question is how to get around the problem of writing something to the console from the assembly (this is where we have control over) while the user, our customer (we have no control over), could also write something on the console.

Categories

Find more on Parallel Computing Fundamentals 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!