Disp in background thread

10 ビュー (過去 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 Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by