DataQueue and batch combined

5 views (last 30 days)
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

Accepted Answer

Edric Ellis
Edric Ellis on 21 Jul 2017
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.

More Answers (0)

Categories

Find more on Asynchronous Parallel Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!