Send and automatically process data
DataQueue object to automatically process data sent from a
function that you run in the background, on a parallel pool, or in your current MATLAB® session.
You can automatically process data sent from a function that you run on a parallel pool if you have Parallel Computing Toolbox™.
When you create a
DataQueue object, you create a connection to the
current MATLAB session that you can use to send and receive messages.
object that can be used to send and automatically process messages.
QueueLength — Number of items waiting to be removed from queue
zero or positive integer
This property is read-only.
Number of items of data waiting to be removed from the queue, specified as a zero or positive integer.
The value is a zero or positive integer in the MATLAB session that creates the
Everywhere else, the value is
For example, if you create a
DataQueue object in the
current MATLAB session, you can run a function in the background with that
DataQueue object as an input argument. The
QueueLength property of that
will always be
0 in the background.
Automatically Process Data Sent from the Background
This example shows how to automatically process data in your current MATLAB session that you send from the background.
DataQueue object. After each item of data is received on the
DataQueue in your current MATLAB session, automatically display that item using the
q = parallel.pool.DataQueue; afterEach(q,@disp);
The helper function
magicWithSend defined at the end of this example sends the sum of a magic square to a
PollableDataQueue object, then returns that magic square.
backgroundPool to run the function
magicWithSend in the background.
f = parfeval(backgroundPool,@magicWithSend,1,q,3);
The sum is displayed before you fetch outputs from the future. To retrieve the output from the background, use
fetchOutputs. MATLAB returns the output once the execution of
magicWithSend is complete.
ans = 3×3 8 1 6 3 5 7 4 9 2
Define Helper Function
Define the helper function
magicWithSend. The function creates a magic square, then sends the sum of the magic square to a
PollableDataQueue object. After the sum is sent, the function returns the magic square.
function X = magicWithSend(q,n) X = magic(n); s = sum(X,'all'); send(q,s); end
Introduced in R2017a
spmd (Parallel Computing Toolbox) |
spmdReceive (Parallel Computing Toolbox) |
spmdSend (Parallel Computing Toolbox)