MATLAB: DataQueue and batch combined

batchmessagequeueparallel computing

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

Best Answer

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.