Disp in background thread

조회 수: 8 (최근 30일)
Tomasz Wyrowinski
Tomasz Wyrowinski 2018년 3월 15일
답변: Michael Schlagmüller 2022년 11월 23일
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
Tomasz Wyrowinski
Tomasz Wyrowinski 2018년 3월 16일
Nice and clean idea, thank you.

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

추가 답변 (1개)

Michael Schlagmüller
Michael Schlagmüller 2022년 11월 23일
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에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by