DataQueue and batch combined

7 ビュー (過去 30 日間)
Tomasz Wyrowinski
Tomasz Wyrowinski 2017 年 7 月 20 日
コメント済み: Tomasz Wyrowinski 2017 年 7 月 21 日
Hi, I'd like to run a scipt in background and return some values during its execution. I am currently trying use MessageQueue object to communicate 'caller' and worker thread which works fine while using it together with parfeval. However, my code is a script so I am starting it by using batch function which produces following error when background code tries to send data to MessageQueue:
Error: Struct contents reference from a non-struct array object.
Error Stack: AbstractDataQueue>AbstractDataQueue.send (line 135)
DataQueue>DataQueue.send (line 83)
LoopScript (line 7)
Warnings: While loading an object of class 'parallel.pool.DataQueue':
To send and receive messages there must be a pool.
Here's how I start my script together with MessageQueue communication:
% Calling background script
queue = parallel.pool.DataQueue;
queue.afterEach(@handle_message);
ws = struct('queue', queue);
job = batch('LoopScript', 'Workspace', ws);
LoopScript
% Code run in backgorund
while true
randVal = floor(rand * 4);
switch randVal
case 0
queue.send(true);
case 1
queue.send(1);
case 2
queue.send('32we');
case 3
queue.send(string([2 9 0]));
end
pause(1);
end
Is it possible to use MessageQueue along with batch function and pass data from background to foreground thread?
Regards

採用された回答

Edric Ellis
Edric Ellis 2017 年 7 月 21 日
It's not possible to use DataQueue with a batch job in this way - DataQueue can only be used to communicate between client and workers in a parallel pool. So, what you could do is this:
parpool(1); % Only 1 worker required
queue = parallel.pool.DataQueue;
queue.afterEach(@handle_message);
future = parfeval(@LoopFunction, 0, queue);
where you need to modify your LoopScript to be a function so that it can work with parfeval.
  1 件のコメント
Tomasz Wyrowinski
Tomasz Wyrowinski 2017 年 7 月 21 日
Works great.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by