Disp in background thread

14 次查看(过去 30 天)
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).

采纳的回答

Edric Ellis
Edric Ellis 2018-3-16
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

更多回答(1 个)

Michael Schlagmüller
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.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by